简体   繁体   中英

C# mongodb insert inside a document in a collection

Following is the structure of doc.

{
    "name" : "Apparel & Accessories",
    "description" : "Apparel & Accessories",
    "logoPath" : "apparel_n_accessories.png",
    "categoryCode" : "APP-N-ACC",
    "isActive" : 1,
    "subCategory" : [
            {
                    "name" : "Clothing",
                    "description" : "Clothing",
                    "logoPath" : "clothing.png",
                    "categoryCode" : "CLOTH",
                    "isActive" : 1,
                    "subCategory" : [
                            {
                                    "name" : "Outerwear",
                                    "description" : "Outerwear",
                                    "logoPath" : "outerwear.png",
                                    "categoryCode" : "OUTWER",
                                    "isActive" : 1,
                                    "subCategory" : [
                                            {
                                                    "name" : "Coats & Jackets",
                                                    "description" : "Coats & Jackets",
                                                    "logoPath" : "coats_n_jackets.png",
                                                    "categoryCode" : "COT-N-JACT",
                                                    "isActive" : 1,
                                                    "subCategory" : [ ]
                                            }
                                    ]
                            },
                            {
                                    "name" : "Jewelry",
                                    "description" : "Jewelry",
                                    "logoPath" : "jewelry.png",
                                    "categoryCode" : "JEWL",
                                    "subCategory" : [
                                            {
                                                    "name" : "Rings",
                                                    "description" : "Rings",
                                                    "logoPath" : "rings.png",
                                                    "categoryCode" : "RINGS",
                                                    "isActive" : 1,
                                                    "subCategory" : [ ]
                                            }
                                    ]
                            }
                    ]
            }
    ]
}

I want to insert into subcategory of "Apparel & Accessories" with following content :

{
                    "name" : "XYZ",
                    "description" : "XYZ",
                    "logoPath" : "XYZ.png",
                    "categoryCode" : "XYZ",
                    "isActive" : 1,
                    "subCategory" : [ ]
            }

We are using c# ver 1.8 legacy drivers to connect mongodb.

Can anyone please suggest how to find any level object and add in it.

Do something like this:

var filter = Builders<Category>
             .Filter.Eq(c => c.name, "Apparel & Accessories");

var update = Builders<Category>.Update
        .Push<Category>(c => c.subCategory, mySubCategory);

await collection.FindOneAndUpdateAsync(filter, update);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM