Home > Enterprise >  how to use compare field values in mongo compass
how to use compare field values in mongo compass

Time:02-24

{
    "Employee": {
        "EmployeeData": {
        
                "basePay": "1000",
                "sumPayAmount": "2000"
            }


    }
},
{
    "Employee": {
        "EmployeeData": {
        
                "basePay": "4000",
                "sumPayAmount": "1000"
            }


    }
}

how to find the documents where basePay is greather than sumPayAmount?

CodePudding user response:

You need $expr to compare both fields. And make sure to convert the value to decimal/integer before compare.

db.collection.find({
  $expr: {
    $gt: [
      {
        $toDecimal: "$Employee.EmployeeData.basePay"
      },
      {
        $toDecimal: "$Employee.EmployeeData.sumPayAmount"
      }
    ]
  }
})

Sample Mongo Playground

  • Related