Elhanan Schwarts
Elhanan Schwarts

Reputation: 383

mongodb aggregate match two fields

A stage in an aggregation pipeline produce the following collection:

{
    _id : {
        airport : "SGF",
        minEffect : "security_delay",
        delayType : "arr_delay",
    }
}
{
    _id : {
        airport : "MIA",
        minEffect : "weather_delay",
        delayType : "weather_delay",
    }
}
{
...
}

I want to reduce the documents in the collection and to get only the documents where the value of minEffect equal to the value of delayed Type.

Why the following code doesn't work?

{$match:{"_id.delayType":"$_id.minEffect"}}

In reference to the sample above, the expected result need to be as follows:

{
    _id : {
        airport : "MIA",
        minEffect : "weather_delay",
        delayType : "weather_delay",
    }
}
{
...
}

Upvotes: 1

Views: 116

Answers (1)

varman
varman

Reputation: 8894

You can use like following.

{
    $match: {
      $expr: {
        $eq: [
          "$_id.minEffect",
          "$_id.delayType"
        ]
      }
    }
  }

Working Mongo playground

Upvotes: 1

Related Questions