这是我正在使用的内容:
{
"_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": [],
}
我想要的是每个文档的计数,这些文档具有一定范围内的“实例日期”(比如说一个月)以及某个名称。我使用了两个带有“elemMatch”的查询,不幸的是我没有得到我期望的结果。这是我尝试使用 elemMatch 和 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()
这是一个嵌套的 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()
提前致谢。