2015-04-27 65 views
0

我有一個MongoDB的文檔:如何在MongoDB文檔的特定級別插入數據?

{ 
    "_id" : ObjectId("553e0c2ec9ad2dbe13cc17cc"), 
    "country" : "ireland", 
    "games" : [ 
      { 
        "title" : "Test", 
        "draws" : [ ] 
      }, 
      { 
        "title" : "Test 2", 
        "draws" : [ ] 
      }, 
      { 
        "title" : "Test 3", 
        "draws" : [ ] 
      } 
    ] 
} 

我想存儲下結構相同的多個對象繪製關鍵。現在,如果我想返回測試的比賽,我將執行以下蒙戈查詢:

db.data.find({country:"ireland"},{games:{$elemMatch:{title:"Test"}}}) 

...返回:

{ 
    "_id" : ObjectId("553e0c2ec9ad2dbe13cc17cc"), 
    "games" : [ 
      { 
        "title" : "Test", 
        "draws" : [ ] 
      } 
    ] 
} 

我如何將數據輸入到繪製關鍵?這裏是什麼,我希望獲得一個例子:

{ 
    "_id" : ObjectId("553e0c2ec9ad2dbe13cc17cc"), 
    "country" : "ireland", 
    "games" : [ 
      { 
        "title" : "Test", 
        "draws" : [{ 
         "date":"20150207", 
         "numbers":[10,20,30,40,50,60] 
        }, 
        { 
         "date":"20150214", 
         "numbers":[09,07,22,31,39,30] 
        }, 
        { 
         "date":"20150221", 
         "numbers":[11,07,22,13,30,01] 
        }] 
      }, 
      { 
        "title" : "Test 2", 
        draws : [{ 
         "date":"20150207", 
         "numbers":[03,08,21,19,24,01] 
        }, 
        { 
         "date":"20150214", 
         "numbers":[19,17,02,11,19,32] 
        }, 
        { 
         "date":"20150221", 
         "numbers":[31,27,12,10,15,11] 
        }] 
      }, 
      { 
        "title" : "Test 3", 
        draws : [{ 
         "date":"20150207", 
         "numbers":[10,17,29,33,31,11] 
        }, 
        { 
         "date":"20150214", 
         "numbers":[12,17,19,33,31,02] 
        }, 
        { 
         "date":"20150221", 
         "numbers":[01,17,32,23,31,01] 
        }] 
      } 
    ] 
} 

我已經試過更新()UPSERT但它一直每次寫一個新的文檔。

+1

如果你想添加的每個'title'與當時使用蒙戈單更新查詢不同'draws'數組值是不可能的,你應該遍歷'games'數組,然後添加值借鑑。更多幫助[檢查此](http://stackoverflow.com/questions/27498872/how-to-change-datatype-of-netsted-field-in-mongo-document) – Yogesh

+0

感謝@yogesh的快速反應。我在我想要的JSON中使用了錯誤的格式。如果你能看到上面的更新,那正是我期望得到的。 – dazziola

+0

這看起來和你以前的輸出一樣,這種情況如果你知道'games.title:Test',那麼你應該添加'title:Test'的'draws'數組。因此,添加三個'draws'你應該使用三個不同的查詢來更新單個文檔。所以更好的方式來使用一些編程編碼或使用mongo'bulk'操作 – Yogesh

回答

0

我用下面的代碼插入一個子文檔。

db.data.update({country:"ireland",games:{$elemMatch:{title:"Test"}}}, 
     { 
      $push: { 
       'games.$.draws': [ 

       {"date":"20150207", 
       "numbers":[10,20,30,40,50,60]} 


       ]} 

      }) 

結果如下。

{ 
    "_id" : ObjectId("553e2a32f077cac2cbb9a033"), 
    "country" : "ireland", 
    "games" : [ 
     { 
      "title" : "Test", 
      "draws" : [ 
       [ 
        { 
         "date" : "20150207", 
         "numbers" : [ 
          10, 
          20, 
          30, 
          40, 
          50, 
          60 
         ] 
        } 
       ] 
      ] 
     }, 
     { 
      "title" : "Test 2", 
      "draws" : [] 
     }, 
     { 
      "title" : "Test 3", 
      "draws" : [] 
     } 
    ] 
} 
+0

這不是有效答案,查看問題和評論 – Yogesh