Mongoose $ pushAll会覆盖当前的数组对象

时间:2013-04-26 00:43:40

标签: javascript node.js mongodb mongoose

所以我有这个文件,我试图更新评论,当我这样做时,似乎用新信息覆盖了数组。任何想法为什么?

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 个答案:

答案 0 :(得分:0)

试试这个

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