1 varMongostring ="Mongodb://jamesbing:[email protected]:27017";2 varHost =NewTmongodbhostmodel3 {4CollectionName ="ft_test",5ConnectionString =mongostring,6DatabaseName ="system_db"7 };8 9 varperson =New PersonTen { OneId =Objectid.generatenewid (), AName ="jamesbing", -Address =NewList<addressitem> - { the NewAddressitem {Add ="United Kingdom", Id = Objectid.generatenewid (), age = A}, - NewAddressitem {Add ="United States", Id = Objectid.generatenewid (), age = +} - } - }; + //var addresult = tmongodbhelper.add (host, person); - varCollection = Tmongodbhelper.getmongocollection<person>(host. ConnectionString, host. DatabaseName, host. CollectionName); + A //Update childdocument at varAddress ="address.$"; - varresult =collection. Update ( -Query.and (NewList<imongoquery> - { -Query.eq ("_id",NewObjectId ("577b344a87aebb23c0fc767a")), -Query.eq ("address._id",NewObjectId ("577b344a87aebb23c0fc767b")), in}), Update.set (address +". Age", -)); - to + //ADD childdocument - //single the varRESULT2 = collection. Update (Query.eq ("_id",NewObjectId ("577b2d6787aebb198839d415") *), Update.push ("Address",NewAddressitem {Add ="Shanxi Province Transport City", Id =Objectid.generatenewid ()}. Tobsondocument ())); $ Panax Notoginseng //multiple - varRESULT3 = collection. Update (Query.eq ("_id",NewObjectId ("577B262587AEBB41FCA9A3AF") the), Update.pusheach ("Address",New[] {NewAddressitem {Add ="4 Yuncheng, Shanxi province", Id = Objectid.generatenewid (), age =Ten}. Tobsondocument (),NewAddressitem {Add ="5 Yuncheng, Shanxi province", Id = Objectid.generatenewid (), age =9 }. Tobsondocument ()})); + A //Remove childdocument the varRESULT4 = collection. Update (Query.eq ("_id",NewObjectId ("577b2d6787aebb198839d415")), +Update.pull ("Address",New{Id =NewObjectId ("577b2d9387aebb2d7449367f") }. Tobsondocument ())); - $ $ //Query childdocument - varQ = Query.and (New[] -{Query.eq ("_id",NewObjectId ("577b30e087aebb34ec170e7a")), Query.eq ("ADD","Beijing")}); the varQresult = collection. Find (Query.elemmatch ("Address", Q)). FirstOrDefault (); -Console.readkey ();
Note:
MongoDB for C # SDK version: 1.10
MongoDB version: 3.0.6
C # manipulating MongoDB sub-documents