1

This is my document in MongoDB:

{
    "_id": {
        "$oid": "566193b0c9b5290f234242"
    },
    "name": "fake-name-1",
    "profiles": [
        {
            "real-name": "fake-name-1",
            "color": "fake-color-1"
        },
        {
            "real-name": "fake-name-2",
            "color": "fake-color-2",
            "active": true
        },
        {
            "real-name": "fake-name-3",
            "color": "fake-color-3"
        }
    ]
}

I'm real newbie to MondoDb, and are trying to find the document where profiles contains a real-name with "MArtin43221" and active = true.

How do I create a search query for this?

I've tried:

{"profiles": ["real-name":"MArtin43221", "active":true]}
Michael Nielsen
  • 1,194
  • 3
  • 22
  • 37
  • Use the [dot notation](https://docs.mongodb.org/manual/tutorial/query-documents/#match-a-field-without-specifying-array-index). You'll use `db.collection.find({"profiles.real-name":"MArtin43221", "profiles.active":true"})` – Abdullah Rasheed Dec 04 '15 at 13:37
  • This will give documents where real-name and active are spread across profile array. Wont get the 'and' feeling – Sarath Nair Dec 04 '15 at 13:40
  • How do I get the 'and' feeling? – Michael Nielsen Dec 04 '15 at 13:42
  • Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – styvane Dec 04 '15 at 13:50

1 Answers1

1

Try elemMatch:

db.collection.find({"profiles": {$elemMatch:{"real-name":"MArtin43221", "active":true}}})
Sarath Nair
  • 2,828
  • 2
  • 21
  • 36