我在每个产品上循环使用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);你知道我怎样才能解决这个问题吗?
误差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] }不走运。

下面是来自蒙戈官方文档的例子
{ "_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表示两个文档中的动态值。但就我而言,"$totalQuantity“不起作用
https://docs.mongodb.com/manual/reference/operator/aggregation/divide/
发布于 2021-01-19 09:13:24
由Prasad_Saya
嗨,@Shanka_Somasiri,我不知道你在尝试什么。例如,下面是使用聚合管道进行更新的方法。
我有一份投入文件:
{
_id: 1,
reservations: [ ]
}这是我想要推入预订数组的一份文件。
var docToUpdate = { loadingShetId: 12, reservedCaseQty: 200 }更新查询:
db.test.updateOne(
{ _id: 1 },
[
{
$set: {
reservations: {
$concatArrays: [ "$reservations", [ docToUpdate ] ]
}
}
}
]
)更新后的文件:
{
"_id" : 1,
"reservations" : [
{
"loadingShetId" : 12,
"reservedCaseQty" : 200
}
]
}其余字段更新:
$inc: { totalQuantity: -el.loadingTotal }
$set: { currentQuantity: "$totalQuantity" }可以按以下方式编码:
totalQuantity: { $add: [ "$totalQuantity", -el.loadingTotal ] }
currentQuantity: "$totalQuantity"因此,更新查询将变成:
db.test.updateOne(
{ _id: 1 },
[
{
$set: {
reservations: {
$concatArrays: [ "$reservations", [ docToUpdate ] ]
},
totalQuantity: { $add: [ "$totalQuantity", -el.loadingTotal ] },
currentQuantity: "$totalQuantity"
}
}
]
)发布于 2021-01-01 14:45:37
问题是,您试图存储"$totalQuantity“的文本,而不是在这一行中存储一个数值:
$set: { currentQuantity: "$totalQuantity" }相反,硬编码的数字值为
$set: { currentQuantity: 5 }将值设置为5。现在,您需要了解动态值是什么。如果您有一个以数字作为值的$totalQuantity变量,那么您可以这样做:
$set: { currentQuantity: $totalQuantity }如果不是,那么您将需要弄清楚如何访问所需的值,并使用这些知识来访问它。
https://stackoverflow.com/questions/65529892
复制相似问题