MongoDB NumberLong在PHP中显示

时间:2016-07-06 06:21:01

标签: php mongodb

目前我正在处理MongoDB收集数据到PHP表,我的MongoDB集合中有以下数据

> db.AETOM.find().pretty()
{
    "_id" : {
        "UCODE" : NumberLong("413010192215382"),
        "DCODE" : NumberLong("990004908640390")
    },
    "Total" : 14
}
{
    "_id" : {
        "UCODE" : NumberLong("413010192171534"),
        "DCODE" : NumberLong("990004393658160")
    },
    "Total" : 562
}
{
    "_id" : {
        "UCODE" : NumberLong("413011193047063"),
        "DCODE" : NumberLong("990004298968470")
    },
    "Total" : 99
}

这是我用来显示记录的代码

<?php

        $m = new MongoClient();
        $db = $m->selectDB('MapData');
        $collection = new MongoCollection($db,'AETOM');

        $cursor = $collection->find();
        $cursor->limit(10);
        echo "<html><head></head><body>";
        $data  = "<table style='border:1px solid red;";
        $data .= "border-collapse:collapse' border='1px'>";
        $data .= "<thead>";
        $data .= "<tr>";
        $data .= "<th>UCODE</th>";
        $data .= "<th>DCODE</th>";
        $data .= "<th>Total</th>";
        $data .= "</tr>";
        $data .= "</thead>";
        $data .= "<tbody>";
        foreach($cursor as $document) {
            $data .= "<tr>";
            $data .= "<td>" . $document["UCODE"] . "</td>";
            $data .= "<td>" . $document["DCODE"] . "</td>";
            $data .= "<td>" . $document["Total"]."</td>";
            $data .= "</tr>";
        }
        $data .= "</tbody>";
        $data .= "</table>";
        echo $data;
        echo "</body></html>";
?>

当我运行它时,它只显示“总计”值,UCODE和DCODE字段没有显示任何内容,当我用终端检查它时显示如下错误

  

PHP注意:未定义的索引:UCODE in   第23行的/var/www/html/MongoDBT/test2.php

     

PHP注意:未定义的索引:DCODE in   第22行的/var/www/html/MongoDBT/test2.php

请帮助我解决问题。

1 个答案:

答案 0 :(得分:1)

由于UCODEDCODE来自_id数组,因此要获取它们,您必须执行以下操作: -

$document["_id"]["UCODE"]  instead of $document["UCODE"]

AND

$document["_id"]["DCODE"] instead of $document["DCODE"]