当我试图更新“标题”时,它工作正常,但如果我试图更新数组中的嵌套对象,它就不起作用。
数据: Data
工作:
const restaurant = await Restaurant.update(
{_id: '60e6828e4992a2979fa0ba3e'},
{$set: {title: "Hello}},
);
不工作:
const restaurant = await Restaurant.update(
{_id: '60e6828e4992a2979fa0ba3e', 'grades.grade': 85},
{$set: {'grades.$.grade': 0}},
);
最佳答案
演示 - https://mongoplayground.net/p/caFBz-lx8dQ
使用$[]
The filtered positional operator $[] identifies the array elements that match the arrayFilters conditions for an update operation
db.collection.update(
{},
{ $set: { "grades.$[elem].grade": 0 }},
{ arrayFilters: [ {"elem.grade": 85 } ]}
)
关于javascript - Mongodb 位置运算符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68297703/