[英]MongoDB BulkWrite update operation $set not working
I am looping through products (variable records) with a bulkWrite updateOne operation on each product.我在每个产品上使用 bulkWrite updateOne 操作来遍历产品(变量记录)。
Once I update the records I can see the reservations array is being added to the document, totalQuantity is updated to the expected value (eg: if the totalQuantity is 2000 and the loadingTotal is 600 then the updated totalQuantity is 1400)更新记录后,我可以看到预订数组被添加到文档中,totalQuantity 更新为预期值(例如:如果 totalQuantity 为 2000,loadingTotal 为 600,则更新后的 totalQuantity 为 1400)
As you can see in line $set: { currentQuantity: "$totalQuantity" } I am trying to assign totalQuantity(1400) to currentQuantity.正如您在 $set: { currentQuantity: "$totalQuantity" } 行中看到的,我正在尝试将 totalQuantity(1400) 分配给 currentQuantity。 But this is not working.
但这不起作用。
for (const el of records) {
promiseArray.push(
Stock.bulkWrite(
[
{
updateOne: {
filter: {
index: el.index,
product: el.product,
batchNo: el.batchNo,
agency,
totalQuantity: { $gte: el.loadingTotal },
},
update: {
$push: {
reservations: {
loadingSheetId: sheetAfterSave._id,
reservedCaseQuantity: el.loadingCaseCount,
reservedUnitQuantity: el.loadingUnitCount,
reservedTotalQuantity: el.loadingTotal,
},
},
$inc: { totalQuantity: -el.loadingTotal },
$set: { currentQuantity: "$totalQuantity" } // Issue
},
},
},
],
{ session: session }
)
);
}
const result = await Promise.all(promiseArray);
console.log('******** Result Promise ********', result);
Any idea how I can get this solved??知道如何解决这个问题吗?
ERROR MSG错误信息
[distribution] CastError: Cast to Number failed for value "$totalQuantity" at path "currentQuantity"
[distribution] at SchemaNumber.cast (/app/node_modules/mongoose/lib/schema/number.js:384:11)
[distribution] at SchemaNumber.SchemaType.applySetters (/app/node_modules/mongoose/lib/schematype.js:1031:12)
[distribution] at SchemaNumber.SchemaType._castForQuery (/app/node_modules/mongoose/lib/schematype.js:1459:15)
[distribution] at SchemaNumber.castForQuery (/app/node_modules/mongoose/lib/schema/number.js:436:14)
[distribution] at SchemaNumber.SchemaType.castForQueryWrapper (/app/node_modules/mongoose/lib/schematype.js:1428:15)
[distribution] at castUpdateVal (/app/node_modules/mongoose/lib/helpers/query/castUpdate.js:520:19)
[distribution] at walkUpdatePath (/app/node_modules/mongoose/lib/helpers/query/castUpdate.js:347:22)
[distribution] at castUpdate (/app/node_modules/mongoose/lib/helpers/query/castUpdate.js:94:7)
[distribution] at /app/node_modules/mongoose/lib/helpers/model/castBulkWrite.js:70:37
[distribution] at /app/node_modules/mongoose/lib/model.js:3502:35
[distribution] at each (/app/node_modules/mongoose/lib/helpers/each.js:11:5)
[distribution] at /app/node_modules/mongoose/lib/model.js:3502:5
[distribution] at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
[distribution] at new Promise (<anonymous>)
[distribution] at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
[distribution] at Function.Model.bulkWrite (/app/node_modules/mongoose/lib/model.js:3500:10) {
[distribution] stringValue: '"$totalQuantity"',
[distribution] messageFormat: undefined,
[distribution] kind: 'Number',
[distribution] value: '$totalQuantity',
[distribution] path: 'currentQuantity',
[distribution] reason: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
[distribution]
[distribution] assert.ok(!isNaN(val))
[distribution]
[distribution] at castNumber (/app/node_modules/mongoose/lib/cast/number.js:28:10)
[distribution] at SchemaNumber.cast (/app/node_modules/mongoose/lib/schema/number.js:382:12)
[distribution] at SchemaNumber.SchemaType.applySetters (/app/node_modules/mongoose/lib/schematype.js:1031:12)
[distribution] at SchemaNumber.SchemaType._castForQuery (/app/node_modules/mongoose/lib/schematype.js:1459:15)
[distribution] at SchemaNumber.castForQuery (/app/node_modules/mongoose/lib/schema/number.js:436:14)
[distribution] at SchemaNumber.SchemaType.castForQueryWrapper (/app/node_modules/mongoose/lib/schematype.js:1428:15)
[distribution] at castUpdateVal (/app/node_modules/mongoose/lib/helpers/query/castUpdate.js:520:19)
[distribution] at walkUpdatePath (/app/node_modules/mongoose/lib/helpers/query/castUpdate.js:347:22)
[distribution] at castUpdate (/app/node_modules/mongoose/lib/helpers/query/castUpdate.js:94:7)
[distribution] at /app/node_modules/mongoose/lib/helpers/model/castBulkWrite.js:70:37
[distribution] at /app/node_modules/mongoose/lib/model.js:3502:35
[distribution] at each (/app/node_modules/mongoose/lib/helpers/each.js:11:5)
[distribution] at /app/node_modules/mongoose/lib/model.js:3502:5
[distribution] at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
[distribution] at new Promise (<anonymous>)
[distribution] at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10) {
[distribution] generatedMessage: true,
[distribution] code: 'ERR_ASSERTION',
[distribution] actual: false,
[distribution] expected: true,
[distribution] operator: '=='
[distribution] }
[distribution] }
below example from mongo official docs以下示例来自 mongo 官方文档
{ "_id" : 1, "name" : "A", "hours" : 80, "resources" : 7 },
{ "_id" : 2, "name" : "B", "hours" : 40, "resources" : 4 }
db.planning.aggregate(
[
{ $project: { name: 1, workdays: { $divide: [ "$hours", 8 ] } } }
]
)
{ "_id" : 1, "name" : "A", "workdays" : 10 }
{ "_id" : 2, "name" : "B", "workdays" : 5 }
using $hours to denote the dynamic values in the 2 documents.使用 $hours 表示 2 个文档中的动态值。 but in my case "$totalQuantity" doesnt work
但在我的情况下,“$totalQuantity”不起作用
https://docs.mongodb.com/manual/reference/operator/aggregation/divide/ https://docs.mongodb.com/manual/reference/operator/aggregation/divide/
The problem is that you are trying to store the text of "$totalQuantity" instead of a numeric value in this line:问题是您试图在此行中存储“$totalQuantity”的文本而不是数值:
$set: { currentQuantity: "$totalQuantity" }
Instead of that, a hardcoded numeric value of取而代之的是一个硬编码的数值
$set: { currentQuantity: 5 }
would set the value to 5. Now, you need to find out what the dynamic value is.会将值设置为 5。现在,您需要找出动态值是什么。 If you have a $totalQuantity variable that has a number as value, then you can do something like this:
如果您有一个以数字为值的 $totalQuantity 变量,那么您可以执行以下操作:
$set: { currentQuantity: $totalQuantity }
If not, then you will need to figure out how you can access the value that you need and use that knowledge to access it.如果没有,那么您将需要弄清楚如何访问所需的值并使用该知识来访问它。
By Prasad_Saya通过 Prasad_Saya
Hi @Shanka_Somasiri, I am not sure about what you are trying.嗨@Shanka_Somasiri,我不确定你在尝试什么。 Here is how you do the update using Aggregation Pipeline, for example.
例如,这是使用聚合管道进行更新的方法。
I have an input document:我有一个输入文件:
{
_id: 1,
reservations: [ ]
}
Here is a document I want to push into the reservations array.这是我要推送到预订数组中的文档。
var docToUpdate = { loadingShetId: 12, reservedCaseQty: 200 }
The update query:更新查询:
db.test.updateOne(
{ _id: 1 },
[
{
$set: {
reservations: {
$concatArrays: [ "$reservations", [ docToUpdate ] ]
}
}
}
]
)
The updated document:更新后的文件:
{
"_id" : 1,
"reservations" : [
{
"loadingShetId" : 12,
"reservedCaseQty" : 200
}
]
}
The remaining field updates:其余字段更新:
$inc: { totalQuantity: -el.loadingTotal }
$set: { currentQuantity: "$totalQuantity" }
Can be coded as follows:可以编码如下:
totalQuantity: { $add: [ "$totalQuantity", -el.loadingTotal ] }
currentQuantity: "$totalQuantity"
So, the update query will become:因此,更新查询将变为:
db.test.updateOne(
{ _id: 1 },
[
{
$set: {
reservations: {
$concatArrays: [ "$reservations", [ docToUpdate ] ]
},
totalQuantity: { $add: [ "$totalQuantity", -el.loadingTotal ] },
currentQuantity: "$totalQuantity"
}
}
]
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.