mongo db aggregate match using value from collection

Viewed 312

How can I match a field in MongoDB with value from another field of the same collection.

For example from the collection test: (not exactly the same collection but it serves the need)

test:

{
    "a" : "one",
    "b" : "two",
    "c" : "three"
    "d" : "one"
}

I need to fetch document where value of 'a' matches the value of 'd'. I tried:

db.test.aggregate([
    {$match:{a:'$d'}}
])

but no hope!!!

1 Answers
db.collection.aggregate([
  {
    $match: {
      $expr: {
        $eq: [
          "$a",
          "$d"
        ]
      }
    }
  }
])

Working Mongo playground

Related