2016-07-06 83 views
2

目前我在MongoDB收集數據的工作中PHP表,在我MongoDB收集有如下數據MongoDB的NumberLong顯示

> 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在 /var/www/html/MongoDBT/test2.php上線23

PHP說明:未定義指數:DCODE在 /無功/網絡/ HTML/MongoDBT /第22行的test2.php

請親切地幫我解決問題。

+0

嘗試用'$文件[ 「_ ID」 ] [「UCODE」]'和'$ document [「_ id」] [「DCODE」]'並告訴工作與否? –

+1

是的,它工作:),非常感謝你 – Kavinda

+0

Kavinda很高興幫助你。歡呼:) :) :) –

回答

1

由於UCODEDCODE成爲內側_id陣列,所以來接他們,你必須做如下圖所示: -

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

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