Javascript 如何更改mongodb nodejs字段的值?
这是模型的模式:Javascript 如何更改mongodb nodejs字段的值?,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,这是模型的模式: { season: 1, finished: 0, Games: [{ Game: [ { game: 1, Players: [ { Id: 0, name: "Peter", points: 3 },
{
season: 1,
finished: 0,
Games: [{
Game: [
{
game: 1,
Players: [
{
Id: 0,
name: "Peter",
points: 3
},
{
Id: 1,
name: "Yusuf",
points: 2
}
]
},
{
game: 2,
Players: [
{
Id: 0,
name: "Peter",
points: 2
},
{
Id: 1,
name: "Yusuf",
points: 3
}
]
}
]
}]
}
我尝试了底部的查询,但它没有按我想要的那样工作。我只想更改所选游戏的Game
字段,但它会在同一季节将所有Game
游戏的所有字段更改为相同的值
Seasons.update(
{season:1, "Games.Game.game":2},
{$set: {
Games: {
Game: {
game: 5
}
}
}},
(err, data) => {
if (err) {
console.log(err)
} else {
console.log(data)
}
}
)
问题是“如何选择和更新记录子数组的值?”您可以使用arrayFilters,标识与更新操作的arrayFilters条件匹配的数组元素
- ,位置运算符指示更新运算符应修改指定数组字段中的所有元素
$[g]
指阵列过滤器条件
很有效,谢谢你,伙计。但我不能完全理解这个案子。你能给我一个关于这个话题的来源吗?我已经附上了回答$[]和$[]的链接,你可以点击这个链接。
await Seasons.update(
{
season: 1,
"Games.Game.game": 2
},
{
$set: {
"Games.$[].Game.$[g].game": 5
}
},
{
arrayFilters: [
{ "g.game": 2 }
]
},
(err, data) => {
if (err) {
console.log(err)
} else {
console.log(data)
}
}
)