Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to do projection with simple subtract function but I am receiving error on $subtract mongodb can't $subtract string from string

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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'}

  ]
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading