C # manipulating MongoDB sub-documents

Source: Internet
Author: User
Tags mongodb version

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.