mongodb - MongoDB BulkWrite 更新操作 $set 不工作
问题描述
我在每个产品上使用 bulkWrite updateOne 操作来遍历产品(变量记录)。
更新记录后,我可以看到预订数组被添加到文档中,totalQuantity 更新为预期值(例如:如果 totalQuantity 为 2000,loadingTotal 为 600,则更新后的 totalQuantity 为 1400)
正如您在 $set: { currentQuantity: "$totalQuantity" } 行中看到的,我正在尝试将 totalQuantity(1400) 分配给 currentQuantity。但这不起作用。
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);
知道如何解决这个问题吗?
错误信息
[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] }
以下示例来自 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 }
使用 $hours 表示 2 个文档中的动态值。但在我的情况下,“$totalQuantity”不起作用
https://docs.mongodb.com/manual/reference/operator/aggregation/divide/
解决方案
问题是您试图在此行中存储“$totalQuantity”的文本而不是数值:
$set: { currentQuantity: "$totalQuantity" }
取而代之的是一个硬编码的数值
$set: { currentQuantity: 5 }
会将值设置为 5。现在,您需要找出动态值是什么。如果您有一个以数字为值的 $totalQuantity 变量,那么您可以执行以下操作:
$set: { currentQuantity: $totalQuantity }
如果没有,那么您将需要弄清楚如何访问所需的值并使用该知识来访问它。
推荐阅读
- reactjs - 在 map 函数中使用三元组
- go - 从 `metrics:` 字段标签配置 Prometheus Go 客户端
- node.js - peerjs 事件“呼叫”有时不会启动
- html - 右侧幻灯片活动且更大的 jQuery 轮播
- scalardb - ScalarDB中没有clusterKey的Get返回什么
- angular - 数据未以角度显示加载时间
- c++ - 使用具有原子谓词但没有互斥锁的 C++ 条件变量
- c++ - 获取学生的平均年龄和最小学生的名字
- laravel - laravel 使用刀片显示数据库中的图像
- javascript - vue.js 在对象数组中呈现一个列表