Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying key-value map in mongo [duplicate]

Tags:

mongodb

I have documents stored in mongo database following this schema:

{
   map:{
         key1:value,
         banana:value2 
         ....
        }

}

How can I query objects based on keys in this map ?

e.g I want to get all the documents which map contains key that equals banana.

like image 400
Marcin Majewski Avatar asked Jul 31 '15 12:07

Marcin Majewski


1 Answers

Maps are accessed the same way as normal nested values. This means that you can use the $exists operator to check if the key exists.

db.collection.find( { "map.banana" : { $exists : true } } );
like image 54
Rakesh Goyal Avatar answered Sep 19 '22 12:09

Rakesh Goyal