Obtaining results from MongoDB with PHP (working with command line)
I'm trying to obtain a simple collection doing the next:
<?php
echo "<pre>";
$mongo = new Mongo();
var_dump($mongo);
$db = $mongo->testdb;
var_dump($db);
$cursor = $db->users->find();
var_dump($cursor);
?>
And I get the next output:
object(Mongo)#1 (4) {
["connected"]=>
bool(true)
["status"]=>
NULL
["server":protected]=>
string(0) ""
["persistent":protected]=>
NULL
}
object(MongoDB)#2 (2) {
["w"]=>
int(1)
["wtimeout"]=>
int(10000)
}
object(MongoCursor)#4 (0) {
}
IF i开发者_运维百科n the command line I do this:
use testdb;
db.users.find();
I obtain:
{ "_id" : ObjectId("4e7fca596803fa4b53000000"), "username" : "test4", "password" : "md5pass", "email" : "test4@email.tld", "group" : 1, "profile_fields" : "a:0:{}" }
{ "_id" : ObjectId("4e7fca6e6803fa4a53000000"), "username" : "test4", "password" : "md5pass", "email" : "test4@email.tld", "group" : 1, "profile_fields" : "a:0:{}" }
{ "_id" : ObjectId("4e7fca726803fa4a53000001"), "username" : "test4", "password" : "md5pass", "email" : "test4@email.tld", "group" : 1, "profile_fields" : "a:0:{}" }
{ "_id" : ObjectId("4e7fca736803fa4a53000002"), "username" : "test4", "password" : "md5pass", "email" : "test4@email.tld", "group" : 1, "profile_fields" : "a:0:{}" }
{ "_id" : ObjectId("4e7fcae26803fa4c53000000"), "username" : "test4", "password" : "md5pass", "email" : "test4@email.tld", "group" : 1, "profile_fields" : "a:0:{}" }
{ "_id" : ObjectId("4e7fcb076803fa4953000000"), "username" : "test1", "password" : "md5pass", "email" : "test1@email.tld", "group" : 1, "profile_fields" : "a:0:{}" }
What I'm doing wrong?
Thank you in advance!
Do a foreach
on the Cursor in your first example.
The reason being you do not see anything in the var_dump
is that it is a resource
. Resources are pointers, they do not hold real data by themselves.
精彩评论