how to get data from mongodb collection by match data in subdocument? -


i have mongodb collection data

   var data = [ {     name: 'test1',     attributes: [         {             name: 'color',             value: 'red'         },         {             name: 'size',             value: 'l'         }     ] }, {     name: 'test2',     attributes: [         {             name: 'color',             value: 'blue'         },         {             name: 'size',             value: 's'         }     ] }, {     name: 'test3',     attributes: [         {             name: 'color',             value: 'red'         },         {             name: 'size',             value: 's'         }     ] } 

]

how query database return documents matching attribute name 'color' having value 'red' , attribute name 'size' having value 'l'?

means should return

 var output = [ {     name: 'test1',     attributes: [         {             name: 'color',             value: 'red'         },         {             name: 'size',             value: 'l'         }     ] } 

]

you can make use of $and operator in find query.

db.collection.find({$and:[{"attributes.name":"color","attributes.value":"red"},                            {"attributes.name":"size","attributes.value":"l"}]}) 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -