我正在使用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的东西呢?
尝试
->field('other')->equals(new \MongoId("516c0061975a299edc44b419"))
ObjectId是Mongo的内部类型,由PHP中的\ MongoId()表示
(但我也在第一个主题中回答)