Home > Net >  MongoDB Aggregation - Parse field to decimal and use this in match
MongoDB Aggregation - Parse field to decimal and use this in match

Time:03-26

I have a document that has a balance field of type string.

I want to rule out all of these documents where the "balance" property is $lte: 0 but I can't manage to find the right syntax to do so.

$match:{
  {$toDecimal:'$balance'}: $lte:0
}

It's not working. I'm wondering how to cast the field to a numeric inside a match function before making the match itself.

CodePudding user response:

You should use $expr and the syntax should be as below:

{
  $match: {
    $expr: {
      $lte: [
        { $toDecimal: '$balance' },
        0
      ]
    }
  }
}

Sample Mongo Playground

  • Related