我有一个使用MongoDB 7.0.5社区、Mongoose 7.6.8、MongoDB驱动程序6.3.0和Node版本20.10.0的 node 应用程序.
我无法使此代码工作:
const updatedDealer = await my_model.updateMany(
{
CustomerId: 123,
'Vehicles.Id': { $in: IdArray }
},
{
$set: { 'Vehicles.$.FieldsToUpdate': true }
},
{
multi: true
}
);
从字面上看,Id数组是一个数字array.我的Mongoose模型是正确的,因为如果我使用以下命令更新单个记录:
const updatedDealer = await my_model.findOneAndUpdate(
{
CustomerId: 123,
'Vehicles.Id': 123456
},
{
$set: { 'Vehicles.$.FieldsToUpdate': true }
},
{
new: true
}
);
它起作用了. 下一个CustomerID或Vehicles.ID是MongoDB_ID.
这是我的数据的简化示例:
[{
CustomerId: 123,
CustomerName: 'Some Company',
Vehicles: [
{
Id: 123456,
Make: 'Ford',
Model: 'Focus',
FieldsToUpdate: false
},
{
Id: 543213,
Make: 'Kia',
Model: 'EV9',
FieldsToUpdate: false
}
]
}]
下面是我的模型的一个简化示例:
const my_model = mongoose.Schema({
CustomerId: {
type: Number
},
CustomerName: {
type: String
},
Vehicles: [
{
Id: {
type: Number
},
Make: {
type: String
},
Model: {
type: String
},
FieldsToUpdate: {
type: Boolean
}
}
]
})
希望你能帮忙.谢谢.