我正在尝试使用简单的减法函数进行投影,但我在 $subtract mongodb can't $subtract string from string 上收到错误

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

我正在尝试对 mongodb 做减法,但我遇到了格式问题

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

我想在 MongoDB Compass

上执行这个查询

我收到的错误是

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

您的值是字符串格式。

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.

您需要即时将字符串转换为整数 $toInt

PureProfit: {
  $subtract: [

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

  ]
 }