简体   繁体   English

C#mongodb插入集合中的文档内部

[英]C# mongodb insert inside a document in a collection

Following is the structure of doc. 以下是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. 我们正在使用c#ver 1.8旧版驱动程序来连接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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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