通过id查找Doctrine MongoDB

 -起司Cheese- 发布于 2023-02-04 20:27

我正在使用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")
}

我现在如何查询其他是给定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等于objectId的东西呢?

1 个回答
  • 尝试

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

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

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

    2023-02-04 20:33 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有