Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find in double nested array

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.

like image 522
Ken Avatar asked May 01 '26 00:05

Ken


1 Answers

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

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

MongoPlayground

like image 128
Ashh Avatar answered May 02 '26 20:05

Ashh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!