duanrenchuo9244 2018-09-26 18:27
浏览 158
已采纳

PHP中的MongoDB聚合

I'm trying to make an aggregate using PHP. My Mongo script is:

var start = ISODate("2018-06-01T00:00:00.000Z").getTime() / 1000;
var end = ISODate("2018-06-30T23:59:59.000Z").getTime() / 1000;

print("nodo;cantidad");
db.reclamosTecnicos.aggregate([

    { $match : { tipoResolucion: {$eq: 0}, fechaCarga: { $gt: start, $lt: end } } },
    { $group : { _id : "$cliente.nodoCrm" , cantidad : {$sum : 1} } }

]).forEach(function(doc){
print(doc._id+";"+doc.cantidad);
})

Which gives me the following result:

enter image description here

My PHP code is the following:

$start = new MongoDB\BSON\UTCDateTime(strtotime("01-06-2018"));
    $end = new MongoDB\BSON\UTCDateTime(strtotime("30-06-2018"));

    $filter= [
        ['$match' => 
            [
               'tipoResolucion' => array('$eq' => 0), 
                'fechaCarga' => array(
                    '$gte' => $start, 
                    '$lte' => $end
                )
            ]
        ],
        ['$group' => ['_id' => '$cliente.nodoCrm', 'cantidad' => ['$sum' => 1]]]
    ];

    $cursor =   (new MongoDB\Client('mongodb://cantpost the address'))->moica->reclamosTecnicos->aggregate($filter);

    foreach ($cursor as $reclamo) {
        printf("%s tiene %d RTs
", $reclamo['_id'], $reclamo['cantidad']);
    }

My problem is that is not giving me any result. Do you have any clue of what I might be doing wrong?

Thank you!

UPDATE: I checked that $start and $end are in the proper format and they do (they are timestamp, like the records). I did a var_dump() and got the following result:

object(MongoDB\Driver\Cursor)#7 (10) { ["database"]=> string(5) "moica" ["collection"]=> NULL ["query"]=> NULL ["command"]=> object(MongoDB\Driver\Command)#6 (1) { ["command"]=> object(stdClass)#18 (4) { ["aggregate"]=> string(16) "reclamosTecnicos" ["pipeline"]=> array(2) { [0]=> object(stdClass)#13 (1) { ["$match"]=> object(stdClass)#9 (2) { ["tipoResolucion"]=> object(stdClass)#12 (1) { ["$eq"]=> int(0) } ["fechaCarga"]=> object(stdClass)#8 (2) { ["$gt"]=> object(MongoDB\BSON\UTCDateTime)#10 (1) { ["milliseconds"]=> string(10) "1527804000" } ["$lt"]=> object(MongoDB\BSON\UTCDateTime)#5 (1) { ["milliseconds"]=> string(10) "1530309600" } } } } [1]=> object(stdClass)#16 (1) { ["$group"]=> object(stdClass)#15 (2) { ["_id"]=> string(16) "$cliente.nodoCrm" ["cantidad"]=> object(stdClass)#14 (1) { ["$sum"]=> int(1) } } } } ["allowDiskUse"]=> bool(false) ["cursor"]=> object(stdClass)#17 (0) { } } } ["readPreference"]=> object(MongoDB\Driver\ReadPreference)#11 (1) { ["mode"]=> string(7) "primary" } ["session"]=> NULL ["isDead"]=> bool(false) ["currentIndex"]=> int(0) ["currentDocument"]=> NULL ["server"]=> object(MongoDB\Driver\Server)#4 (10) { ["host"]=> string(34) "CANT POST THE ADDRESS" ["port"]=> int(27017) ["type"]=> int(2) ["is_primary"]=> bool(false) ["is_secondary"]=> bool(false) ["is_arbiter"]=> bool(false) ["is_hidden"]=> bool(false) ["is_passive"]=> bool(false) ["last_is_master"]=> array(9) { ["ismaster"]=> bool(true) ["msg"]=> string(8) "isdbgrid" ["maxBsonObjectSize"]=> int(16777216) ["maxMessageSizeBytes"]=> int(48000000) ["maxWriteBatchSize"]=> int(1000) ["localTime"]=> object(MongoDB\BSON\UTCDateTime)#18 (1) { ["milliseconds"]=> string(13) "1538756257349" } ["maxWireVersion"]=> int(5) ["minWireVersion"]=> int(0) ["ok"]=> float(1) } ["round_trip_time"]=> int(4) } } 

I hope this could give you a hint of what I'm doing wrong.

This a document example:

This is a sample document:

{
    "_id" : ObjectId("5b396b0b51b99745e229b818"),
    "idReclamo" : 9428849,
    "cliente" : {
        "idCliente" : 4690204,
        "nombre" : "NOMBRE",
        "apellido" : "PAZ",
        "calle" : "CALLE",
        "nro" : 2943,
        "piso" : null,
        "depto" : null,
        "ciudad" : "BS.AIRES",
        "localidad" : "INGENIERO MASCHWITZ",
        "partido" : "ESCOBAR",
        "nodoCrm" : "ES12",
        "hub" : "ES",
        "region" : "NORTE",
        "geocodificacion" : {
            "idgeo" : 1582517,
            "lat" : 0,
            "lng" : 0
        },
        "nodoRed" : {
            "nodo" : [
                "ES12B"
            ],
            "nodoTxt" : "ES12B",
            "nodoCmts" : "ES12B",
            "hostname" : "TI9-CMTS",
            "hub" : "TI",
            "region" : "Norte",
            "macdomain" : 1292
        },
        "infoCm" : [
            {
                "mac" : "5890436203ed",
                "estado" : "up",
                "niveles" : {
                    "pwrDs" : 3.4,
                    "pwrUs" : 46,
                    "snrDs" : 41.4,
                    "snrUs" : 35
                },
                "repositorio" : {
                    "mac" : "5890436203ed",
                    "firstseen" : 1530109321,
                    "lastseen" : 1530484521,
                    "vendor" : "SAGEMCOM",
                    "modelo" : "FAST3890V2",
                    "hwrev" : "V2.0",
                    "swrev" : "FAST3890_TLC_50.10.15.T2",
                    "sysORDescr" : "An agent which supports all MIBs required by the DOCSIS 3.1 OSS specification.",
                    "docsisVersion" : "D3.1",
                    "lastversioncheck" : 1530484521,
                    "ultimoCliente" : 4690204
                },
                "timestamp" : 1530489611
            }
        ],
        "infoCmCant" : 1
    },
    "motivoVisita" : {
        "id" : 263,
        "descripcion" : "CATV S/S CLIENTE.BA/TEL/CATV"
    },
    "tipoResolucion" : 0,
    "telefono" : "0111564521333",
    "jerarquia" : 1,
    "estado" : "P",
    "observacion" : "Abn S/S ctv, se reinicia se rebootea no responde. Sr. Ezequie. 1564521333 ",
    "serviciosAfectados" : {
        "catv" : true,
        "bandaAncha" : false,
        "telefonia" : false,
        "uhf" : false
    },
    "fechaCarga" : 1530489595,
    "fechaCargaStr" : "2018-07-01",
    "fechaCargaDate" : ISODate("2018-07-01T23:59:55Z"),
    "usuarioCarga" : {
        "id" : 40467,
        "descripcion" : "FPINEIRO-ATE FPINEIRO-ATE"
    },
    "fechaCierre" : null,
    "fechaCierreStr" : null,
    "fechaCierreDate" : null,
    "usuarioCierre" : null,
    "cierre" : null,
    "fechaUltModif" : 1530489595,
    "fechaUltModifStr" : "2018-07-01",
    "fechaUltModifDate" : ISODate("2018-07-01T23:59:55Z"),
    "usuarioUltModif" : {
        "id" : 40467,
        "descripcion" : "FPINEIRO-ATE FPINEIRO-ATE"
    },
    "otrs" : null,
    "historialMovimientos" : null,
    "timestampSincr" : 1530489611
}

Thanks!

  • 写回答

1条回答 默认 最新

  • dtt27783 2018-10-05 18:16
    关注

    Thanks to Veeram, the solution was to use strtotime(.) instead of MongoDB\BSON\UTCDateTime(strtotime(.))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决