【问题标题】:Null set returned by mongodb aggregate Cursor in PHPPHP中mongodb聚合游标返回的空集
【发布时间】: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


【解决方案1】:

尝试生成一个MongoDate()对象如下

$dateFrom = new MongoDate(strtotime("2015-06-01T00:00:00Z"));
$dateTo = new MongoDate(strtotime("2015-06-03T00:00:00Z"));

然后您可以在聚合管道中使用它,而不是 PHP 查询中的 MongoDB ISODate 对象。

/* Run the command cursor */
$result = $collection->aggregateCursor(
    [
        [ '$match' => [ 'date'=> [ '$gte' => $dateFrom, '$lte' =>  $dateTo ]  ] ],
        [ '$group' => [ '_id' => '$date', 'count' => [ '$sum' => 1 ] ] ]            
    ]        
);

【讨论】:

  • 嗨 Chridam ,使用 MongoDate() 完成更改,但现在它会引发如下超时错误。未捕获的异常“MongoCursorTimeoutException”,消息“192.x.x.x:10002:读取 0 字节后读取超时,在 C:\wamp\www\subscriber_details\cman.php 第 71 行等待 30.000000 秒”
  • @SaikumarA 在调用aggregateCursor() 之前尝试设置MongoCursor::$timeout = -1(-1 没有超时)。更多信息请参考documentation
  • 大家好,谢谢。得到了预期的结果。
猜你喜欢
  • 2016-08-10
  • 2015-07-23
  • 1970-01-01
  • 2019-05-28
  • 1970-01-01
  • 2020-10-01
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多