Mongoose:我如何在数组属性上找到OneAndAndUpdate并推入找到的数组属性?

时间:2017-01-28 22:01:16

标签: arrays node.js mongodb mongoose

鉴于以下文件:

{
    "_id" : ObjectId("588d121dc23324935a061108"),
    "game" : "/donut_fun",
    "__v" : 0,
    "rooms" : [
        {
            "members" : [ ],
            "timestamp" : 1485640221059,
            "roomname" : "fun times"
        }
    ]
}

如何使用game通过roomnamefindOneAndUpdate()属性进行查询,并将对象推送到members数组中。

我在尝试(没有成功):

var player = {pid: 1234, pname: 'Fred'};
var query = {game: '/donut_fun', roomname: 'fun times'};
var update = {$push: {'rooms.$.members':player }};
GamesModel.findOneAndUpdate(query, update, {new: true, upsert:true}, function(err, doc){
  console.log(doc);  
});

0 个答案:

没有答案