簡體   English   中英

C#mongodb插入集合中的文檔內部

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

以下是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" : [ ]
                                            }
                                    ]
                            }
                    ]
            }
    ]
}

我要插入“服裝和配飾”的子類別,其內容如下:

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

我們正在使用c#ver 1.8舊版驅動程序來連接mongodb。

任何人都可以建議如何找到任何關卡對象並添加它。

做這樣的事情:

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