我有类似的架构:
const UserSchema = new mongoose.Schema({
skills: [{
_id: false,
name: String,
level: Number,
exp: Number
}]
});
示例文档如下:
{
"_id": {
"$oid": "5b0941419703f80a121c44df"
},
"skills": [
{
"name": "woodcutting",
"exp": 1110,
"level": 1
},
{
"name": "smithing",
"level": 0,
"exp": 0
},
{
"name": "reading",
"level": 0,
"exp": 0
},
{
"name": "writing",
"level": 0,
"exp": 0
}
]
}
因此,我只需要在一个查询中更新exp和木刻与锻造的水平,如果可能的话,怎么可能? 还是我需要更改所有数组并设置(替换)技能值?
答案 0 :(得分:1)
您可以在mongodb 3.6中使用arrayFilters
db.collection.update(
{ },
{ "$set": {
"skills.$[skill1].exp": "your_value",
"skills.$[skill2].level": "your_value",
"skills.$[skill2].exp": "your_value",
"skills.$[skill1].level": "your_value"
}},
{ "arrayFilters": [{ "skill1.name": "woodcutting", "skill2.name": "smithing" }] }
)