2014-01-09 169 views
2

我使用ODM蒙戈学说找到,我要记录类主义的MongoDB通过ID

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();  

这将产生一个错误的蒙戈查询

MongoDB的查询: { “发现”:真正的 “查询”:{ “其他”: “的ObjectId(516c0061975a299edc44b419)”}, “田” :[], “DB”: “maself”, “收藏”: “东西”} [] []

当我使用

- >字段( '其他') - >等于( “516c0061975a299edc44b419”)

查询也有错

MongoDB的查询: { “发现”:真正的 “查询”:{ “其它”: “516c0061975a299edc44b419”}, “田”:[], “DB”: “maself”, “收藏”: “东西”} [] []

所以,我怎么能寻找的东西其他id等于objectId?

回答

4

尝试

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

objectid是蒙戈内部类型,由\ MongoId()在PHP表示

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