Home > Enterprise >  Is it possible to create a query matching the same document on MongoDB?
Is it possible to create a query matching the same document on MongoDB?

Time:07-02

I have a document that looks like this:

{
    "total": 150.00,
    "products": [
        {"a": 75.00},
        {"b": 75.00}
    ],
    "paid": 100.00
}

I'm trying to find a way to query a document where the total is greater than the paid field.

So far I've tried:

db.find(...)

{paid: {$lt: "$total"}}
{paid: {$lt: "$total"}}
{paid: {$lt: "$$this.total"}}

{total: {$gt: "$paid"}}
{total: {$gt: "$$this.paid"}}

{$where: "this.total > this.paid"}

CodePudding user response:

You need to use $expr expression operator to match internal fields and $gt aggregation operator to check condition,

{ $expr: { $gt: ["$total", "$paid"] } }
  • Related