Doctrine MongoDB find by id



我正在使用odm mongo学说,我必须记录类

class Thing
{
/**
 * @MongoDBId
 */
protected $id;
 /**
  * @MongoDBReferenceOne(targetDocument="Bundle1:Other")
  */
protected $other;
}

class Other
{
/**
 * @MongoDBId
 */
protected $id;
}

所以在数据库中看起来像:

{
  "_id":ObjectId("43z758634875adf"),
  "other":ObjectId("38z287348d8se")
}

我现在如何查询其他是给定 id 的内容?

    $dm=$this->mongo->getManager();
            $answers=$dm
                ->createQueryBuilder('Bundle1:Thing')
                ->field('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 等于对象 ID 的东西呢?

试试

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

ObjectId 是 Mongo 的内部类型,在 PHP 中由 \MongoId() 表示

(但我在第一个题目中也回答过)

最新更新