Reputation: 75
I have the following code which should return an array of personnel, but it keeps returning the client object. I followed the documentation. I have checked that the data exist using mongo shell. I'm not sure what I'm doing wrong.
Mongodb: 2.4.9
MongoClient: 1.1
PHP: 7.0
$collection = (new MongoDB\Client)->intranet->personnel;
$personnel = $collection->find([]);
var_dump($personnel);
This is the result I'm getting
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)
}
}
Upvotes: 0
Views: 252
Reputation: 1012
the content of your $personnel
variable is a MongoCursor
object and NOT what you are expecting to be a result array. in order to retrieve values based on your query, you have to iterate each item on that MongoCursor
.
e.g.
<?php
$collection = (new MongoDB\Client)->intranet->personnel;
$result = $collection->find();
$personnels = [];
foreach ($result as $personnel) {
// do something to each document
$personnels[] = $personnel;
}
var_dump($personnels);
or you can also use PHP's iterator_to_array
function to convert it directly to array.
<?php
$collection = (new MongoDB\Client)->intranet->personnel;
$personnels = iterator_to_array($collection->find());
var_dump($personnels);
Upvotes: 1