0

I have JSON like this:

{
    "array": [
        {
            "object1": {
                "key1":"value1",
                "key2":"value2",
                "key3":"value3"
            }
        },
        {
            "object2": {
                "key1":"value1",
                "key2":"value2",
                "key3":"value3"
            }
        }
    ]
}

I want to get to the key inside object in MongoDB by db.collection.find(), but I can't figure out what to type inside parentheses. I tried: db.collection.find({"array":{"object1.key1":"value1"}});but it doesn't work.

  • 2
    Try `db.collection.find({"array.object1.key1":"value1"})`. This will return the document where there is match on that key. To return the matched array element you use `db.collection.find({"array.object1.key1":"value1"}, {array.$":1})` – s7vr Aug 21 '18 at 12:41
  • It's working, but you forgot about quotation mark: `{"array.$":1})` Thanks a lot! ;) – Przemek Sulecki Aug 21 '18 at 13:00

0 Answers0