2013-05-07 72 views
0

你好,我對mongodb相當陌生,誠實,因爲我習慣於使用mysql,並且對json的熟悉程度也較低。用Mongodb將一個元素推到一個數組中的陣列

這裏是從集合討論

{ 
     "_id" : ObjectId("5188c93f0361ca6dc33e3a30"), 
     "admin" : [ ], 
     "created" : "2013-04-30 19:10:21", 
     "description" : "guitar theory", 
     "members" : [ ], 
     "modified" : "2013-04-30 19:10:21", 
     "name" : "Arpeggios", 
     "posts" : [ 
       { 
         "post_id" : "1", 
         "user_id" : "1", 
         "name" : "Test", 
         "slug" : "xxx", 
         "comment" : "xxx", 
         "created" : "xxx", 
         "modified" : "xxx", 
         "comments" : [ ], 
         "attachments" : [ ] 
       }, 
       { 
         "post_id" : "2", 
         "user_id" : "1", 
         "name" : "Test", 
         "slug" : "xxx", 
         "comment" : "xxx", 
         "created" : "xxx", 
         "modified" : "xxx", 
         "comments" : [ ], 
         "attachments" : [ ] 
       } 
     ], 
     "profile_pic" : "adasdad", 
     "settings" : [ ], 
     "slug" : "arpeggio" 
} 

我的目標是要在其上POST_ID = 1的陣列註釋推的元素,讓畫面是這樣的結果,我的文檔我想:

{ 
     "_id" : ObjectId("5188c93f0361ca6dc33e3a30"), 
     "admin" : [ ], 
     "created" : "2013-04-30 19:10:21", 
     "description" : "guitar theory", 
     "members" : [ ], 
     "modified" : "2013-04-30 19:10:21", 
     "name" : "Arpeggios", 
     "posts" : [ 
       { 
         "post_id" : "1", 
         "user_id" : "1", 
         "name" : "Test", 
         "slug" : "xxx", 
         "comment" : "xxx", 
         "created" : "xxx", 
         "modified" : "xxx", 
         "comments" : [ 
         {"comment_id":"xxx", "user_id":"xxx", "name":"xxx","comment":"xxx", "created":"xxx", "modified":"xxx"}, 
         {"comment_id":"xxx", "user_id":"xxx", "name":"xxx","comment":"xxx", "created":"xxx", "modified":"xxx"} 
         ], 
         "attachments" : [ ] 
       }, 
       { 
         "post_id" : "2", 
         "user_id" : "1", 
         "name" : "Test", 
         "slug" : "xxx", 
         "comment" : "xxx", 
         "created" : "xxx", 
         "modified" : "xxx", 
         "comments" : [ ], 
         "attachments" : [ ] 
       } 
     ], 
     "profile_pic" : "adasdad", 
     "settings" : [ ], 
     "slug" : "arpeggio" 
} 

我已經徹底研究了幾個小時已經,這是我想出了這僅僅是一個失敗並不起作用:

db.discussions.update(
       {_id:ObjectId("5188c93f0361ca6dc33e3a30"), posts:{post_id:1}}, 
       {$push: 
        {"posts: 
         {comments: 
          {"comment_id":"xxx", "user_id":"xxx", "name":"xxx","comment":"xxx", "created":"xxx", "modified":"xxx"} 
         } 
        } 
       } 
       ) 

請大家我拼命地尋求幫助。我想了解這一點。

+1

將查詢謂詞更改爲「posts.post_id」:1並將$ push部分更改爲「posts。$。comments」:{new comment}這是使用位置運算符:http://docs.mongodb。 org/manual/reference/operator/positions /然而,你真的不應該使用這個模式 - 你的文檔增長是無限的,並且不利於性能。 – 2013-05-07 23:20:50

+0

我已經試過了,但我會再試一次。儘管我會把它傳遞給我的同事,但你的意見。謝謝 – electricfeel1979 2013-05-07 23:40:08

回答

1

有幾件事情正在進行。

首先你想使用the $ positional operator

其次,你提到你的集合名稱是Discussion,但是你的更新使用「討論」 - 確保你使用與實際集合名稱相同的名稱。

第三,您列出的文檔中的post_id是「1」,但您試圖匹配1.字符串「1」不會等於數字1.請注意您的類型。

這句法(注意我也糾正不平衡報價)應該這樣做:

db.discussion.update(
      {_id:ObjectId("5188c93f0361ca6dc33e3a30"), "posts.post_id":"1"}, 
      {$push: 
        {"posts.$.comments": 
         {"comment_id":"xxx", "user_id":"xxx", 
          "name":"xxx","comment":"xxx", "created":"xxx", 
          "modified":"xxx"} 
        } 
       } 
      ) 

我要重申,我對這個模式的厭惡 - 你是不是邊界您的文檔的增長,這將導致性能問題,更不用說使它比你需要的更復雜(也更大)。

+0

在我的node.js我宣佈它是討論,我知道這一點,但謝謝你的提示。無論如何,在我將問題發佈到stackoverflow之前,我已經嘗試過,只是沒有正確使用它。我的錯誤並不是把「」放在「1」上。這對我來說是一個新課程。謝謝! – electricfeel1979 2013-05-08 07:51:03

相關問題