[英]Doctrine MongoDB find by id
我正在使用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()表示
(但我也在第一個主題中回答)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.