【发布时间】:2015-06-19 12:43:35
【问题描述】:
MongoDB 聚合查询返回空集。以下是我在 php 脚本中用于从 mongoDB 检索数据的查询。请告诉我哪里出错了。
$result = $collection->aggregateCursor([[ '$match'=> [ 'date'=> [ '$gte'=>ISODate("2015-06-01T00:00:00Z"), '$lte'=>ISODate("2015-06-03T00:00:00Z")] ] ],[ '$group'=> [ '_id'=> '$date', 'count'=> [ '$sum'=>1 ] ] ]]);
如果我在 mongoDB shell 中运行相同的查询。它会按预期显示输出。
db.mnumber.aggregate([{ $match: { date: { $gte:new ISODate("2015-06-01T00:00:00Z"), $lte:new ISODate("2015-06-03T00:00:00Z") } } },{ $group: { _id: "$date", 'count': { $sum:1 } } }])
{ "_id" : ISODate("2015-06-01T00:00:00Z"), "count" : 10000 }
{ "_id" : ISODate("2015-06-02T00:00:00Z"), "count" : 10000 }
{ "_id" : ISODate("2015-06-03T00:00:00Z"), "count" : 10000 }
收集中的样本数据:
{
"_id" : ObjectId("55743941789a9abe7f4af3fd"),
"msisdn" : "1234567890",
"act_date" : ISODate("2014-11-24T00:00:00Z"),
"date" : ISODate("2015-06-07T00:00:00Z"),
"recharge_stats" : {
"recharge_amt" : 0,
"rechargetype" : "WEB"
},
"voice_usage" : {
"local_og_mou" : 20,
"local_other_mobile_og_mou" : 0,
"nld_og_mou" : 0,
"nld_other_mobile_og_mou" : 10
},
"gprs_usage" : {
"total_access_count" : 1,
"total_datavolume_mb" : 42
},
"sms_usage" : {
"freesms" : 3,
"local_sms_count" : 0,
"nat_sms_count" : 0,
"inter_sms_count" : 0
},
"campaign_details" : {
"camp_id" : "M01124",
"message" : "Hello .",
"msg_id" : "9174051951412054925609431100",
"cmp_activation_status" : "YES"
}
}
【问题讨论】:
-
不喜欢 PHP 数组语法,也不完全精通最新的修订语法(所以不确定那里的结构)。但主要是,我认为MongoDate 是这里使用的 MongoDB 日期构造函数,而不是
ISODate,我根本找不到任何 PHP 参考。也许你自己的功能?
标签: php mongodb mongodb-query pymongo