Mongodb php 使用 _id 搜索会出错

Mongodb php searching using _id gives errors

我想使用 executeQuery mongodb 驱动程序搜索 _id php。

这是我的 users 集合

的文档结构
{
    "_id" : ObjectId("55ad0bd1032e1b12088b46a8"),
    "email" : "abc@abc.com"
}

我的 php 代码是

<?php
//Getting object id
$id = new MongoId("55ad0bd1032e1b12088b46a8");
//filtering 
$filter = ['_id' =>$id];

$options = [];

// Adding query
$query = new MongoDB\Driver\Query($filter, $options);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$cursor = $manager->executeQuery('db.users', $query);

当我 运行 它时,我遇到了以下错误

PHP Fatal error: Uncaught exception 'MongoDB\Driver\Exception\ConnectionException' with message 'unknown operator: $id' in /test.php:27 Stack trace: 0 /test.php(27): MongoDB\Driver\Manager->executeQuery('db.users', Object(MongoDB\Driver\Query)) 1 {main} thrown in test.php on line 27

有什么帮助吗?

根据@Felipe Sulser 的评论

$id = new MongoId("55ad0bd1032e1b12088b46a8");

应该是

$id = new MongoDB\BSON\ObjectId("55ad0bd1032e1b12088b46a8");

现在可以使用了