3

I have a document like this:

{ 
   "_id":ObjectId("5b306824a1eab22e77858c88"),
   "data":{ 
      "Key":[ 
         [ 
            "1529587723",
            "KeyIn"
         ],
         [ 
            "1529587723",
            "Num"
         ],
         [ 
            "1529667745",
            "KeyIn"
         ]
      ]
   },
   "devicecode":"MP1D1XAH@LENOVO"
}

my question is how to find all records with "KeyIn"? I tried

db.dataup.find({ "data.key": "KeyIn" })

but it doesn't work.

1 Answer 1

4

You need to use double nested $elemMatch to find in double nested arrays

db.collection.find({
  "data.Key": {
    "$elemMatch": {
      "$elemMatch": { "$in": ["KeyIn"] }
    }
  }
})

MongoPlayground

Sign up to request clarification or add additional context in comments.

1 Comment

A perfect piece of query!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.