Reputation: 1589
I have a collection with two documents:
{
"type":"s",
"disp":[
{
"quantity":1,
"date":20141109
},
{
"quantity":1,
"date":20141110
}
]
},
{
"type":"d",
"disp":[
{
"quantity":1,
"date":20141109
},
{
"quantity":0,
"date":20141110
}
]
}
I have to make a query (with one or more dates) to retrieve a document where all the given dates have a quantity greater than 0.
So if I search for a date 20141109 then I have to retrieve both but searching for 20141110 or both dates 20141109 and 20141110 should return only type "s" as it's the only with a quantity bigger than 0 for every dates used as parameters
P.S. sorry for my English, it's not my first language
Upvotes: 0
Views: 149
Reputation: 11671
To do such a match for one date, use $elemMatch
:
db.test.find({ "disp" : { "$elemMatch" : { "quantity" : { "$gt" : 0 }, "date" : 20141109} } })
For multiple dates you will need to combine queries like the above using $and
:
db.test.find({ "$and" : [
{ "disp" : { "$elemMatch" : { "quantity" : { "$gt" : 0 }, "date" : 20141109 } } },
{ "disp" : { "$elemMatch" : { "quantity" : { "$gt" : 0 }, "date" : 20141110 } } }
] })
Upvotes: 2