I am looping through products (variable records) with a bulkWrite updateOne operation on each product.
Once I update the records I can see the reservations array is being added to the document, totalQuantity is updated to the expected value (e.g: if the totalQuantity is 2000 and the loadingTotal is 600 then the updated totalQuantity is 1400)
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);
As you can see in line $set: { currentQuantity: “$totalQuantity” } I am trying to assign latest totalQuantity value (1400) to currentQuantity after $inc operation. But this is not working. Getting below error
[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] }
Can someone help me with this issue?? Thanks