2016-03-15 42 views
0

以下是doc的結構。C#mongodb插入集合中的文檔裏面

{ 
    "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#1.8版本舊的驅動程序連接的MongoDB。

任何人都可以請建議如何找到任何級別的對象,並添加它。

回答

0

做這樣的事情:

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

我想用烏爾DLL版本2+。我們使用1.8遺留下來的dll。任何想法如何做到這一點? – Jatin

相關問題