英文:
$Subtract and other artihmetic operators returning Object instead of int
问题
问题
- 我编写了一些代码,用于执行算术操作,因为我正在开发结算软件。
- 这段代码片段导致了问题。
await shopCollection.updateOne(
{ Barcode: elem.Barcode },
{
$set: {
Quantity: { $subtract: ["$Quantity", elem.Quantity] },
SalesQuantity: {
$add: [{ $ifNull: ["$SalesQuantity", 0] }, elem.Quantity],
},
Profit: { $subtract: ["$SalesPrice", "$CostPrice"] },
TotalProfit: {
$add: ["$TotalProfit", { $multiply: ["$Profit", elem.Quantity] }],
},
SalesTotal: {
$add: ["$SalesTotal", { $multiply: ["$SalesPrice", elem.Quantity] }],
},
},
},
{ upsert: true, returnOriginal: false }
);
完整代码:<https://pastebin.com/b6Zzhv16>
它返回的是对象,而不是整数,如下图所示
- 我正在使用 Ubuntu,运行 MongoDB 社区版,而且我没有使用 Mongoose 或模式。
英文:
Problem
- I made some code to perform arithmetic operations as I am working on billing software.
- This code snippet is causing issues
await shopCollection.updateOne(
{ Barcode: elem.Barcode },
{
$set: {
Quantity: { $subtract: ["$Quantity", #elem.Quantity] },
SalesQuantity: {
$add: [{ $ifNull: ["$SalesQuantity", 0] }, elem.Quantity],
},
Profit: { $subtract: ["$SalesPrice", "$CostPrice"] },
TotalProfit: {
$add: ["$TotalProfit", { $multiply: ["$Profit", elem.Quantity] }],
},
SalesTotal: {
$add: [
"$SalesTotal",
{ $multiply: ["$SalesPrice", elem.Quantity] },
],
},
},
},
{ upsert: true, returnOriginal: false }
);
Entire code : <https://pastebin.com/b6Zzhv16>
It returns a object instead of Int like so
- I am using ubuntu ,running the mongodb community edition and I am not using mongoose or schemas
答案1
得分: 2
为了避免这个问题,您将需要使用聚合管道和updateOne
操作。除此之外,我发现了一些问题:
- 由于您在
$set
内创建了Profit
字段,并且尝试在相同的$set
中引用该字段,您将不得不在新的管道$set
阶段中执行此操作。 - 由于初始数据中没有
TotalProfit
利润字段,您将不得不在添加之前编写一个ifNull
检查,否则最终将得到null
。
await shopCollection.updateOne(
{
Barcode: elem.Barcode
},
[
{
$set: {
Quantity: { $subtract: ["$Quantity", elem.Quantity] },
SalesQuantity: { $add: [{ $ifNull: ["$SalesQuantity", 0] }, elem.Quantity] },
Profit: { $subtract: ["$SalesPrice", "$CostPrice"] },
SalesTotal: { $add: ["$SalesTotal", { $multiply: ["$SalesPrice", elem.Quantity] }] }
}
},
{
$set: {
TotalProfit: { $add: [{ $ifNull: ["$TotalProfit", 0] }, { $multiply: ["$Profit", elem.Quantity] }] }
}
}
],
{ upsert: true, returnOriginal: false }
);
英文:
to avoid this you will have to use aggregation pipeline with the updateOne
operation.
Apart from that few issues I found
- Since you are creating the
Profit
field inside the$set
and you are trying to refer to that field in the same$set
you will have to do in a new pipeline$set
stage. - Since you don't have
TotalProfit
profit field in your initial data you will have to write anifNull
check before adding otherwise you end up withnull
await shopCollection.updateOne({
Barcode: elem.Barcode
},
[
{
$set: {
Quantity: { $subtract: [ "$Quantity", elem.Quantity ] },
SalesQuantity: { $add: [ { $ifNull: [ "$SalesQuantity", 0 ] }, elem.Quantity ] },
Profit: { $subtract: [ "$SalesPrice", "$CostPrice" ] },
SalesTotal: { $add: [ "$SalesTotal", { $multiply: [ "$SalesPrice", elem.Quantity ] } ] }
}
},
{
$set: {
TotalProfit: { $add: [ { $ifNull: [ "$TotalProfit", 0 ] }, { $multiply: [ "$Profit", elem.Quantity ] } ] }
}
}
],
{ upsert: true, returnOriginal: false }
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论