Moti Shaul
Moti Shaul

Reputation: 83

MongoDB: how to get specific elements from array?

I have objects in the document that look's like:
my collections
I want to get only the elements from the 'transactions' array that their inside field 'isMatched' == false
I tried:

db.myCollection.find(
{ "transactions.isMatched": false } ,
    { "transactions.isMatched": 1 
    })

But i got all the array elements:
results

What is the appropriate query for this?

Upvotes: 1

Views: 38

Answers (1)

varman
varman

Reputation: 8894

You can achieve this with aggregation. $filter helps to eliminate unwanted objects.

db.collection.aggregate([
  {
    $project: {
      company: 1,
      transaction: {
        $filter: {
          input: "$transaction",
          cond: {
            $eq: [
              "$$this.isMatched",
              false
            ]
          }
        }
      }
    }
  }
])

Working Mongo playground

Upvotes: 1

Related Questions