Here is what I am working with:
{
"_id": ObjectId("53b4402ae1382335c95952d3"),
"created": ISODate("2014-07-02T10:23:54.245Z"),
"modified": ISODate("2011-11-28T10:06:25.186Z"),
"source": [{
"instances": [{
"analyst": "harry",
"date": ISODate("2014-07-02T10:23:54.242Z"),
"reference": "abb32"
},
{
"analyst": "david",
"date": ISODate("2014-07-02T10:33:02.296Z"),
"reference": "can26"
}],
"name": "Apples"
},
{
"instances": [{
"analyst": "critsAPI",
"date": ISODate("2011-11-28T10:06:25.156Z"),
"reference": "z561c"
}],
"name": "Oranges"
}],
"releasability": [],
}
What I would like is a count of every document which has an "instances date " in a certain range (lets say a month) along with a certain name. I have used two queries with "elemMatch" and unfortunately I am not getting the results I expect. Here is what I have tried using elemMatch and instances.date:
collection.find(
{
'source':
{
'$elemMatch' : {
'name':'Apples',
'instances.date' : {'$lte' : ISODate("2014-07-30T00:00:00Z") ,
'$gte' : ISODate("2014-07-01T00:00:00Z")}
}
}
}
).count()
Here is with a nested elemMatch :
collection.find(
{
'source':
{
'$elemMatch' : {
'name':'Apples',
'instances': {
'$elemMatch' : {
'date' : {'$lte' : ISODate("2014-07-30T00:00:00Z") ,
'$gte' : ISODate("2014-07-01T00:00:00Z")}
}
}
}
}
}
).count()
Thanks in advance.