2

Having document in collection test as follow:

{a:2, list:[{lang:"en", value:"Mother"}, {lang:"de", value:"Mutter"}] }

When I query:

db.test.find({"list.lang":"de", "list.value": "Mother" })

I'm expecting to get nothing, but on reason that exist document with 2 nested entries that satisfies total condition MongoDB resolves {a:2}

How to fix query to retrieve only documents where both inner fields satisfies to specified condition simultaneously?

Dewfy
  • 23,277
  • 13
  • 73
  • 121
  • 2
    I can think of a query that uses `$all` as `db.test.find({ "list": { "$all": [{"lang":"de", "value": "Mother" }] } })` – chridam Sep 19 '16 at 16:35
  • 2
    What about [$elemMatch](https://docs.mongodb.com/manual/reference/operator/query/elemMatch/) `db.test.find({ "list": { "$elemMatch": {"lang":"de", "value": "Mother" } } })` – Ali Dehghani Sep 19 '16 at 16:44

1 Answers1

4

Using $elemMatch:

db.test.find({ "list": { "$elemMatch": {"lang":"de", "value": "Mother" } } })

Using $all:

db.test.find({ "list": { "$all": [{"lang":"de", "value": "Mother" }] } })
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
4J41
  • 5,005
  • 1
  • 29
  • 41