findOneAndUpdate-更新数组中具有特定属性的第一个对象 [英] findOneAndUpdate - Update the first object in array that has specific attribute
本文介绍了findOneAndUpdate-更新数组中具有特定属性的第一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有属性的对象.这包括具有自己属性的其他对象的数组.
I have an object that has attributes. This includes an array of other Objects that have their own attributes.
Lobby:
--> "a": "b"
--> "c": "d"
--> Players:[
--> 0
--> "x": "23"
--> "status": "ready"
--> 1
--> "x": "54"
--> "status": "open"
--> 2
--> "x": "16"
--> "status": "open"
我该如何将第一个具有"status": "open"
的Player对象(在本例中为1)更新为新对象?
How would I go about updating the first Player Object that has "status": "open"
(in this case 1) to a new object?
例如:
player = {
x: "125",
status: 'joined'
};
推荐答案
我设法找到了可行的解决方案!
I managed to find a working solution!
Game.findOneAndUpdate({'a': 'b', 'Players.status': 'open'}, { $set : { 'Players.$': player} },
function (err, doc) {
if (err) {
console.log(err);
} else {
// Do stuff
}
});
这篇关于findOneAndUpdate-更新数组中具有特定属性的第一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文