【发布时间】:2021-01-01 13:02:29
【问题描述】:
我正在通过对每个产品执行 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/
【问题讨论】:
-
怎么不工作了?您收到错误消息吗?
-
@LajosArpad 我收到上述错误。 (编辑了问题)。 totalQuantity 和 currentQuantity 在 mongoose Schema 中具有 Number 类型。我不知道为什么这是一个问题:(