Php MongoDB按id查找的原则
我正在使用odm mongo理论,我必须记录类Php MongoDB按id查找的原则,php,mongodb,symfony,doctrine-orm,Php,Mongodb,Symfony,Doctrine Orm,我正在使用odm mongo理论,我必须记录类 class Thing { /** * @MongoDB\Id */ protected $id; /** * @MongoDB\ReferenceOne(targetDocument="Bundle1:Other") */ protected $other; } 及 所以在数据库中,一个东西看起来像: { "_id":ObjectId("43z758634875adf"), "other":ObjectId("38z28
class Thing
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\ReferenceOne(targetDocument="Bundle1:Other")
*/
protected $other;
}
及
所以在数据库中,一个东西看起来像:
{
"_id":ObjectId("43z758634875adf"),
"other":ObjectId("38z287348d8se")
}
我现在如何查询其他是给定id的东西
$dm=$this->mongo->getManager();
$answers=$dm
->createQueryBuilder('Bundle1:Thing')
->field('other')->equals("ObjectId(516c0061975a299edc44b419)") // <-- ?
->getQuery()
->execute()->count();
$dm=$this->mongo->getManager();
$answers=$dm
->createQueryBuilder('Bundle1:Thing')
->字段('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()表示
(但我也回答了第一个问题)
->field('other')->equals(new \MongoId("516c0061975a299edc44b419"))