Reputation: 1233
i have mongodb collection with data like
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 to query into database so that it will return the documents matching attribute name 'color' having value 'red' and attribute name 'size' having value 'L'?
means it should return
var output = [
{
name: 'test1',
attributes: [
{
name: 'color',
value: 'red'
},
{
name: 'size',
value: 'L'
}
]
}
]
Upvotes: 3
Views: 214
Reputation: 19700
You can make use of the $and
operator in the find
query.
db.collection.find({$and:[{"attributes.name":"color","attributes.value":"red"},
{"attributes.name":"size","attributes.value":"L"}]})
Upvotes: 2