2013-04-26 43 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 } 
       }},