我正在使用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()表示
(但我也回答了第一个主题)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句