SajjadRahnama
SajjadRahnama

Reputation: 43

MongoDB $elemMatch on array

I have a document like this :

{
  "values": 
    [
      ["a","231"],
      ["b","232"],
      ["c","233"],
      ["a","235"]  
    ]
}

How to use $elemMatch on value property. want to project elements in values that first element of them are "a".

something like this:

db.test.find({"values" : {$elemMatch: { "matchedElemet"[0]: "a"}}},{"values.$":1})

Upvotes: 4

Views: 9526

Answers (1)

Pankaj kumar Panigrahi
Pankaj kumar Panigrahi

Reputation: 761

You can use something like this:

db.test.find(
    {
        'values': {
            $elemMatch: {
                $elemMatch: {
                    $in: ['a']
                }
            }
        }
    },
    {
        "values.$": 1
    }
)

or

db.test.find(
    {},
    {
        'values': {
            $elemMatch: {
                $elemMatch: {
                    $in: ['a']
                }
            }
        }
    }
)

I hope I got your requirement correctly and this helps.

Upvotes: 4

Related Questions