【问题标题】:Doctrine MongoDB find by idDoctrine MongoDB 按 id 查找
【发布时间】:2016-02-08 05:51:31
【问题描述】:

我正在使用 odm mongo 原则,我必须记录类

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

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

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

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

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

我现在如何查询 other 是给定 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"} [] []

当我使用时

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

查询也是错误的

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

那么我如何搜索其他 id 等于 objectId 的东西?

【问题讨论】:

    标签: php mongodb symfony doctrine-orm


    【解决方案1】:

    试试

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

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

    (但我在第一个主题中也有回答)

    【讨论】:

    • 我在 ClassMetadata.php 第 515 行出现异常:不能使用 MongoId 类型的对象作为数组
    猜你喜欢
    • 2013-01-23
    • 1970-01-01
    • 2013-02-19
    • 2018-09-18
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多