目前,我正在对toMongoDB
表中的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>";
?>
当我运行它时,它只显示“total”值,ucode和dcode字段不显示任何内容,当我用终端检查它时,它显示如下错误
php注意:未定义的索引:ucode in
/var/www/html/mongodbt/test2.php第23行
php注意:未定义的索引:dcode in
/var/www/html/mongodbt/test2.php第22行
请帮我解决这个问题。
最佳答案
因为UCODE
和DCODE
在_id
数组中,所以要获取它们,必须执行以下操作:
$document["_id"]["UCODE"] instead of $document["UCODE"]
和
$document["_id"]["DCODE"] instead of $document["DCODE"]
关于php - MongoDB NumberLong在PHP中显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38217413/