gpt4 book ai didi

node.js - 批量写入 - 类型错误 : Cannot create property '$set' on number '0' at applyTimestampsToUpdate - mongoose or mongodb

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:19 27 4
gpt4 key购买 nike

我尝试使用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
}
]
},
...
]

最佳答案

我在尝试更新模型架构中不存在的字段时遇到了同样的错误。

可以使用 <model>.collection.bulkWrite 解决该问题或者通过将字段添加到架构中(至少这两个操作在我的情况下都有效)。

关于node.js - 批量写入 - 类型错误 : Cannot create property '$set' on number '0' at applyTimestampsToUpdate - mongoose or mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57490799/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com