使用PHP从MongoDB获取结果(使用命令行)

ude*_*ter 4 php mongodb

我正在尝试获取下一个简单的集合:

<?php
echo "<pre>";
$mongo = new Mongo();
var_dump($mongo);
$db = $mongo->testdb;
var_dump($db);

$cursor = $db->users->find();

var_dump($cursor);
?>
Run Code Online (Sandbox Code Playgroud)

我得到了下一个输出:

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) {
}
Run Code Online (Sandbox Code Playgroud)

如果在命令行中我执行此操作:

use testdb;
db.users.find();
Run Code Online (Sandbox Code Playgroud)

我获得:

{ "_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:{}" }
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

先感谢您!

Ita*_*vka 5

foreach在第一个示例中对光标执行操作.

你没有看到任何东西的原因var_dump是它是一个resource.资源是指针,它们不能自己保存真实数据.