我在我的C#MVC应用程序中使用MongoDB Driver 2.3.0并收到此错误:
"无法反序列化'列表'来自BsonType' Document'"
更新后获取数据 以下是我的代码:
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);
我能够更新记录但是当我尝试使用Linq查询获取记录时,我得到上面提到的错误。下面是我收到错误的代码:
var lList = (from e in objModel.AsQueryable<Model>()
where e._id == ObjectId.Parse(Id)
select e.locations).FirstOrDefault();
任何帮助都将不胜感激。