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
Post a Comment