C# 更新Mongodb子文檔的實現方法
阿新 • • 發佈:2017-08-01
target person mod for builder pda log date() upd
參考資源:
1:mongodb3.2系統性學習——3、update()操作
2: C# 操作mongodb子文檔
代碼如下:
var mongoString = "mongodb://xxxxxxxxxxx:27017"; var host = new TMongodbHostModel { CollectionName = "ft_test", ConnectionString = mongoString, DatabaseName= "system_db" }; var person = new Person { Id = ObjectId.GenerateNewId(), Name = "jamesbing", Address = new List<AddressItem> { new AddressItem {Add = "英國", Id = ObjectId.GenerateNewId(), Age = 12}, new AddressItem {Add = "美國", Id = ObjectId.GenerateNewId(), Age = 19} } }; var collection = TMongodbHelper.GetMongoCollection<Person>(host.ConnectionString, host.DatabaseName, host.CollectionName); //Update ChildDocumentvar address = "Address.$.{0}"; var result = collection.FindAndModify( Query.And(new List<IMongoQuery> { Query.EQ("_id", new ObjectId("577b344a87aebb23c0fc767a")), Query.EQ("Address._id", new ObjectId("577b344a87aebb23c0fc767b")), }), MongoDB.Driver.Builders.Update.Set(string.Format(address, "Age"), BsonValue.Create(33)));
C# 更新Mongodb子文檔的實現方法