Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MongoDB按id查找的原则_Php_Mongodb_Symfony_Doctrine Orm - Fatal编程技术网

Php MongoDB按id查找的原则

Php MongoDB按id查找的原则,php,mongodb,symfony,doctrine-orm,Php,Mongodb,Symfony,Doctrine Orm,我正在使用odm mongo理论,我必须记录类 class Thing { /** * @MongoDB\Id */ protected $id; /** * @MongoDB\ReferenceOne(targetDocument="Bundle1:Other") */ protected $other; } 及 所以在数据库中,一个东西看起来像: { "_id":ObjectId("43z758634875adf"), "other":ObjectId("38z28

我正在使用odm mongo理论,我必须记录类

class Thing
{
/**
 * @MongoDB\Id
 */
protected $id;

 /**
  * @MongoDB\ReferenceOne(targetDocument="Bundle1:Other")
  */
protected $other;
}

所以在数据库中,一个东西看起来像:

{
  "_id":ObjectId("43z758634875adf"),
  "other":ObjectId("38z287348d8se")
}
我现在如何查询其他是给定id的东西

    $dm=$this->mongo->getManager();
            $answers=$dm
                ->createQueryBuilder('Bundle1:Thing')
                ->field('other')->equals("ObjectId(516c0061975a299edc44b419)")  // <-- ?
                ->getQuery()
                ->execute()->count();       
$dm=$this->mongo->getManager();
$answers=$dm
->createQueryBuilder('Bundle1:Thing')
->字段('other')->equals(“ObjectId(516c0061975a299edc44b419)”//getQuery()
->execute()->count();
这会产生错误的mongo查询

MongoDB查询: {“find”:true,“query”:{“other”:“ObjectId(516c0061975a299edc44b419)”},“fields”:[],“db”:“maself”,“collection”:“thing”} [][]

当我使用

->字段(“其他”)->等于(“516c0061975a299edc44b419”)

这个问题也是错误的

MongoDB查询: {“find”:true,“query”:{“other”:“516c0061975a299edc44b419”},“fields”:[],“db”:“maself”,“collection”:“thing”} [][]

那个么我怎样才能搜索其他id等于objectId的东西呢

试试看

->field('other')->equals(new \MongoId("516c0061975a299edc44b419"))
ObjectId是Mongo的内部类型,在PHP中由\MongoId()表示

(但我也回答了第一个问题)

->field('other')->equals(new \MongoId("516c0061975a299edc44b419"))