我尝试使用bulkWrite查询来一次性更新多个不同的文档,客户在更新产品数量详细信息时订购了多个产品。我发现bulkWrite updateOne询问。这个批量数组会一次性更新。
代码:
在更新键值时update: { $set: { 'colorName' : null} }
工作正常。但使用嵌套数组键值 update: { $set: { 'sizes.$.qty' : data.qty} }
不起作用。
poductModel.bulkWrite(arrayValue.map((data) => ({
updateOne: {
filter: { _id: data.productQtyDetailsId, 'sizes.name' : data.sizeName },
update: { $set: { 'sizes.$.qty' : data.qty} }
//working fine
//update: { $set: { 'colorName' : null} }
}
}))).then(err,result => {
})
JSON 数据:
[
{
"qty": 8,
"productId": "5d31567ea23d120f087a9aaf",
"productQtyDetailsId": "5d316373b356873504e78be7",
"sizeName": "4",
"colorName": "green",
},
{
"qty": 5,
"productId": "5d31567ea23d120f087a9aaf",
"productQtyDetailsId": "5d31567ea23d120f087a9ab1",
"sizeName": "4",
"colorName": "blue",
}
]
Mongo DB 数据:
[
{
"_id" : ObjectId("5d316373b356873504e78be7"),
"colorName" : "green",
"productId" : ObjectId("5d31567ea23d120f087a9aaf"),
"sizes" : [
{
"name" : "4",
"qty" : 5.0,
"price" : 1500.0
},
{
"name" : "5",
"qty" : 6.0,
"price" : 1600.0
},
{
"name" : "6",
"qty" : 7.0,
"price" : 1700.0
}
]
}
{
"_id" : ObjectId("5d31567ea23d120f087a9ab1"),
"colorName" : "blue",
"productId" : ObjectId("5d31567ea23d120f087a9aaf"),
"sizes" : [
{
"name" : "4",
"qty" : 5.0,
"price" : 1500.0
},
{
"name" : "5",
"qty" : 6.0,
"price" : 1600.0
},
{
"name" : "6",
"qty" : 7.0,
"price" : 1700.0
}
]
},
...
]
我是一名优秀的程序员,十分优秀!