2013-04-26 94 views
0

所以我有這個文件,即時嘗試更新評論,當我這樣做時,似乎用新信息進來覆蓋數組。任何想法爲什麼?

Confession.findOne({_id: confessionID}, function (err, confession) { 
    if (err){ 
     console.log(err); 
    } else { 

      confession.update(
       {$pushAll: {'comments.commentData': 
        [{comment: req.body.text, commenter: req.body.name }] 
       }}, 
       {upsert: true}, 
       function (err, confession) { 
        if (err) { 
         console.log(err); 

        } else { 
         res.send(confession.comments); 
         console.log(confession); 
        } 
       }); 
      } 

這裏是一個示例文件的要求

{ "__v" : 0, 
    "_id" : ObjectId("5171c2bd47a7510000000002"), 
    "absolves" : { "IDs" : [ null ], 
    "num" : 2 }, 
    "comments" : { "commentData" : [ 
     { "comment" : "asdfas wdfwe cqwe qwe w w", 
     "_id" : ObjectId("5179c9fa79c165e74d000002"), 
     "timeStamp" : Date(1366936058266) }], 
    "num" : 0 }, 
    "condemns" : { "IDs" : [ null ], 
    "num" : 2 }, 
    "facebook" : 1232876940, 
    "removed" : false, 
    "text" : " another confession", 
    "time" : Date(1366409917817) } 
+1

沒有答案,但爲什麼'upsert'?你知道這個文件存在,就像你剛剛通過id找到它一樣。在這個問題上,只要執行'Confession.update({_ id:confessionID},update,callback)'' – numbers1311407 2013-04-26 01:00:07

+0

就可以包含示例文檔,無論是在更改之前還是之後(或兩者),都可以跳過嵌套的異步? – 2013-04-26 01:40:33

+0

我編輯了一個示例文檔,它的數組剛剛被新數據寫入了commentData中。 – PBLC 2013-04-26 15:04:53

回答

0

試試這個

{$push: {'comments.commentData': 
        {comment: req.body.text, commenter: req.body.name } 
       }},