C# 操作mongodb子文檔

來源:互聯網
上載者:User

標籤:

 1           var mongoString = "mongodb://jamesbing:[email protected]:27017"; 2             var host = new TMongodbHostModel 3             { 4                 CollectionName = "ft_test", 5                 ConnectionString = mongoString, 6                 DatabaseName = "system_db" 7             }; 8  9             var person = new Person10             {11                 Id = ObjectId.GenerateNewId(),12                 Name = "jamesbing",13                 Address = new List<AddressItem>14                 {15                     new AddressItem {Add = "英國", Id = ObjectId.GenerateNewId(), Age = 12},16                     new AddressItem {Add = "美國", Id = ObjectId.GenerateNewId(), Age = 19}17                 }18             };19             //  var addresult = TMongodbHelper.Add(host, person);20             var collection = TMongodbHelper.GetMongoCollection<Person>(host.ConnectionString, host.DatabaseName, host.CollectionName);21 22             //Update ChildDocument23             var address = "Address.$";24             var result = collection.Update(25                 Query.And(new List<IMongoQuery>26                 {27                    Query.EQ("_id", new ObjectId("577b344a87aebb23c0fc767a")),28                     Query.EQ("Address._id", new ObjectId("577b344a87aebb23c0fc767b")),29                 }), Update.Set(address + ".Age", 33));30 31 32             //Add ChildDocument33             //單個34             var result2 = collection.Update(Query.EQ("_id", new ObjectId("577b2d6787aebb198839d415")35                ), Update.Push("Address", new AddressItem { Add = "山西省運城市", Id = ObjectId.GenerateNewId() }.ToBsonDocument()));36 37             //多個38             var result3 = collection.Update(Query.EQ("_id", new ObjectId("577b262587aebb41fca9a3af")39              ), Update.PushEach("Address", new[] { new AddressItem { Add = "山西省運城市4", Id = ObjectId.GenerateNewId(), Age = 10 }.ToBsonDocument(), new AddressItem { Add = "山西省運城市5", Id = ObjectId.GenerateNewId(), Age = 9 }.ToBsonDocument() }));40 41             //Remove ChildDocument42             var result4 = collection.Update(Query.EQ("_id", new ObjectId("577b2d6787aebb198839d415")),43                Update.Pull("Address", new { Id = new ObjectId("577b2d9387aebb2d7449367f") }.ToBsonDocument()));44 45 46             //Query ChildDocument47             var q = Query.And(new[]48                {Query.EQ("_id", new ObjectId("577b30e087aebb34ec170e7a")), Query.EQ("Add", "北京市")});49             var qresult = collection.Find(Query.ElemMatch("Address", q)).FirstOrDefault();50             Console.ReadKey();

備忘:

mongodb for C# SDK版本:1.10

mongodb版本:3.0.6

 

C# 操作mongodb子文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.