2017-07-26 40 views
0

我有下面的代碼應該返回一個人員數組,但它不斷返回客戶端對象。我遵循了文檔。我已經使用mongo shell檢查了數據。我不確定我做錯了什麼。獲取對象而不是結果,我做錯了什麼?

的MongoDB:2.4.9

MongoClient:1.1

PHP:7.0

$collection = (new MongoDB\Client)->intranet->personnel; 
$personnel = $collection->find([]); 
var_dump($personnel); 

這是我得到

object(MongoDB\Driver\Cursor)#161 (9) { 
    ["database"]=> 
    string(8) "intranet" 
    ["collection"]=> 
    string(9) "personnel" 
    ["query"]=> 
    object(MongoDB\Driver\Query)#160 (3) { 
    ["filter"]=> 
    object(stdClass)#145 (0) { 
    } 
    ["options"]=> 
    object(stdClass)#162 (0) { 
    }enter code here 
    ["readConcern"]=> 
    NULL 
    } 
    ["command"]=> 
    NULL 
    ["readPreference"]=> 
    object(MongoDB\Driver\ReadPreference)#143 (1) { 
    ["mode"]=> 
    string(7) "primary" 
    } 
    ["isDead"]=> 
    bool(false) 
    ["currentIndex"]=> 
    int(0) 
    ["currentDocument"]=> 
    NULL 
    ["server"]=> 
    object(MongoDB\Driver\Server)#146 (10) { 
    ["host"]=> 
    string(9) "127.0.0.1" 
    ["port"]=> 
    int(27017) 
    ["type"]=> 
    int(1) 
    ["is_primary"]=> 
    bool(false) 
    ["is_secondary"]=> 
    bool(false) 
    ["is_arbiter"]=> 
    bool(false) 
    ["is_hidden"]=> 
    bool(false) 
    ["is_passive"]=> 
    bool(false) 
    ["last_is_master"]=> 
    array(5) { 
     ["ismaster"]=> 
     bool(true) 
     ["maxBsonObjectSize"]=> 
     int(16777216) 
     ["maxMessageSizeBytes"]=> 
     int(48000000) 
     ["localTime"]=> 
     object(MongoDB\BSON\UTCDateTime)#162 (1) { 
     ["milliseconds"]=> 
     string(13) "1501091758421" 
     } 
     ["ok"]=> 
     float(1) 
    } 
    ["round_trip_time"]=> 
    int(4) 
    } 
} 
+0

http://php.net/manual/en/mongodb.tutorial.library.php – zod

+0

是啊,這就是我下面的東西,因爲你可以看到我連接就好了和對象都存在,只是不能得到結果。 –

回答

0

$personnel內容的結果變量是一個MongoCursor對象,而不是你所期望的ng是一個結果數組。爲了根據您的查詢檢索值,您必須重複該MongoCursor上的每個項目。

例如您可以使用PHP的iterator_to_array函數直接將其轉換爲數組。

<?php 

$collection = (new MongoDB\Client)->intranet->personnel; 
$personnels = iterator_to_array($collection->find()); 
var_dump($personnels); 
+0

工作完美,非常感謝。 –