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 ) }
4

1 回答 1

0

尝试这个

{$push: {'comments.commentData':
                    {comment: req.body.text, commenter: req.body.name }
                }},
于 2014-03-27T00:13:22.860 回答