Unable to deserialize 'List <T>' from BsonType 'Document'

I am using MongoDB Driver 2.3.0 in my C # MVC application and getting this error:

Cannot Deserialize List from BsonType Document Document

When receiving data after the update.
Below is my code:

IMongoCollection<Model> objModel = dbHelper.GetCollection<Model>(Model.CollectionName); var query = Builders<Model>.Filter.And( Builders<Model>.Filter.Eq("_id", new ObjectId(Id)), Builders<Model>.Filter.Eq("locations.locationid", objLocation.locationid)); var update = Builders<Model>.Update.Set("locations", objLocation.ToBsonDocument()); var result = objModel.UpdateMany(query, update, new UpdateOptions { IsUpsert = true }); if (objLocation.isdefaultlocation) { var lList = (from e in objModel.AsQueryable<Model>() where e._id == ObjectId.Parse(Id) select e.locations).FirstOrDefault(); lList.Where(i => i.isdefaultlocation == true && i.locationid != objLocation.locationid).ToList() .ForEach(s => s.isdefaultlocation = false); Model onjO = new Model(); onjO.locations = lList; var query1 = Builders<Model>.Filter.Eq("_id", new ObjectId(Id)); var update1 = Builders<Model>.Update.Set("locations", MongoDB.Bson.BsonArray.Create(onjO.ToBsonDocument()["locations"])); objModel.UpdateMany(query1, update1); } return (result.ModifiedCount > 0); 

And I can update the record, but when I try to retrieve the record using the Linq query, I get the above error. Below is the code where I get the error:

 var lList = (from e in objModel.AsQueryable<Model>() where e._id == ObjectId.Parse(Id) select e.locations).FirstOrDefault(); 

Any help would be appreciated.

+5
source share

Source: https://habr.com/ru/post/1260965/


All Articles