无法在 find 方法中将数组作为参数传递
Not able to pass array as argument inside find method
我有这个代码:
require_once __DIR__ . "/vendor/autoload.php";
$collection = (new MongoDB\Client)->google_api->threadContents;
$document = $collection->find(["messages.payload.headers.value"=>"kruno@ulix.com"]);
echo "<pre>";
var_dump($document["id"]);
我正在尝试搜索符合特定条件的所有对象。当我使用 findOne
方法时,这完全符合我的要求,但是当我使用 find 方法查找所有对象而不仅仅是一个对象时,我得到下一个错误:
Fatal error: Uncaught Error: Cannot use object of type
MongoDB\Driver\Cursor as array in...
PHP 的 MongoDB\Driver\Cursor 实现了 Traversable class,因此您可以使用 foreach 循环遍历结果,但不能像使用 [] 语句的数组一样直接访问它。
这样做,
foreach($document as $fields){
print_r($fields);
}
我有这个代码:
require_once __DIR__ . "/vendor/autoload.php";
$collection = (new MongoDB\Client)->google_api->threadContents;
$document = $collection->find(["messages.payload.headers.value"=>"kruno@ulix.com"]);
echo "<pre>";
var_dump($document["id"]);
我正在尝试搜索符合特定条件的所有对象。当我使用 findOne
方法时,这完全符合我的要求,但是当我使用 find 方法查找所有对象而不仅仅是一个对象时,我得到下一个错误:
Fatal error: Uncaught Error: Cannot use object of type MongoDB\Driver\Cursor as array in...
PHP 的 MongoDB\Driver\Cursor 实现了 Traversable class,因此您可以使用 foreach 循环遍历结果,但不能像使用 [] 语句的数组一样直接访问它。 这样做,
foreach($document as $fields){
print_r($fields);
}