PHP中的MongoDB聚合

时间:2018-09-26 18:27:34

标签: php mongodb

我正在尝试使用PHP进行汇总。我的Mongo脚本是:

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);
})

哪个给我以下结果:

enter image description here

我的PHP代码如下:

$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\n", $reclamo['_id'], $reclamo['cantidad']);
    }

我的问题是没有给我任何结果。您是否知道我可能做错了什么?

谢谢!

更新:我检查了$ start和$ end的格式是否正确(它们是时间戳,如记录),并且格式正确。我做了一个var_dump()并得到了以下结果:

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) } } 

我希望这可以给您提示我做错了什么。

这是一个文档示例:

这是一个示例文档:

{
    "_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
}

谢谢!

1 个答案:

答案 0 :(得分:0)

感谢Veeram,解决方案是使用strtotime(。)代替MongoDB \ BSON \ UTCDateTime(strtotime(。))

相关问题