码迷,mamicode.com
首页 > 数据库 > 详细

C# 更新Mongodb子文档的实现方法

时间:2017-08-01 14:42:34      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:mongodb   div   class   str   builder   实现   ring   collect   host   

参考资源:

  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 ChildDocument
             var 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子文档的实现方法

标签:mongodb   div   class   str   builder   实现   ring   collect   host   

原文地址:http://www.cnblogs.com/qiailu/p/7267706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!