BitQueen
BitQueen

Reputation: 835

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

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.

Upvotes: 0

Views: 569

Answers (1)

Yong Shun
Yong Shun

Reputation: 51450

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

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

Sample Mongo Playground

Upvotes: 2

Related Questions