gpt4 book ai didi

php - MongoDB NumberLong 在 PHP 中显示

转载 作者:可可西里 更新时间:2023-11-01 10:42:08 25 4
gpt4 key购买 nike

目前我正在使用 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 Notice: Undefined index: UCODE in /var/www/html/MongoDBT/test2.php on line 23

PHP Notice: Undefined index: DCODE in /var/www/html/MongoDBT/test2.php on line 22

请帮我解决这个问题。

最佳答案

由于 UCODEDCODE 位于 _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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com