Home > Mobile >  I am trying to do projection with simple subtract function but I am receiving error on $subtract mon
I am trying to do projection with simple subtract function but I am receiving error on $subtract mon

Time:05-21

I am trying to do subtract on mongodb but I am facing formatting issue

[{$match: {
 StrategyStatus: 1
}}, {$project: {
 TradingPair: 1,
 BotName: 1,
 TotalFees: 1,
 CapitalAmount: 1,
 TotalRealisedProfit: 1,
 PureProfit: {
  $subtract: [
   '$TotalRealisedProfit',
   '$TotalFees'
  ]
 }
}}]

I am tring to execute this query on MongoDB Compass

The error that I am receiving is

PlanExecutor error during aggregation :: caused by :: can't $subtract string from string

CodePudding user response:

Your values are in string format.

Subtracts two numbers to return the difference, or two dates to return the difference in milliseconds, or a date and a number in milliseconds to return the resulting date.

You need to convert your string on the fly to integer $toInt

PureProfit: {
  $subtract: [

   {$toInt : '$TotalRealisedProfit'},
   {$toInt' : $TotalFees'}

  ]
 }
  • Related