【问题标题】:C# mongodb insert inside a document in a collectionC# mongodb 在集合中的文档内插入
【发布时间】:2016-03-15 13:32:37
【问题描述】:

以下是文档的结构。

{
    "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 legacy 驱动程序来连接 mongodb。

任何人都可以建议如何找到任何关卡对象并添加它。

【问题讨论】:

    标签: c# mongodb mongodb-query mongodb-.net-driver


    【解决方案1】:

    做这样的事情:

    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);
    

    【讨论】:

    • 我猜你使用的是 dll 版本 2+。我们使用的是 1.8 旧版 dll。知道该怎么做吗?
    猜你喜欢
    • 2016-10-14
    • 2016-09-22
    • 2016-02-22
    • 2020-08-17
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多