[英]doctrine odm, find document where id is not object id
在我的 mongodb 中,我有一個集合,其中 id-field 不是 object-Id 我不知道為什么它是這樣構建的,我無法從頭開始更改它,因為可能有很多軟件已經在使用這種模式.
但是,這就是 Icecream 文檔的樣子:
{
"_id": "52d0283ae4b01db941dd763b",
"insertDate": ISODate("2014-01-10T17:04:58.617Z"),
"language": "en",
"profile": ObjectId("50e577602b5e05e74b38a6c8"),
"related": ObjectId("516c0061975a299edc44b419"),
"survey": ObjectId("516c0061975a299edc44b409"),
"version": NumberInt(0)
}
使用 mongoshell 我可以找到它:
db.icecream.find({"_id":"52d0283ae4b01db941dd763b"})
而不是使用:
db.icecream.find({"_id":ObjectId("52d0283ae4b01db941dd763b")})
所以我嘗試了很多查詢來找到它,但學說 odm 總是沒有正確的查詢,這是我的最新嘗試:
return $this->mongo->getManager()
->getRepository('DocumentBundle:Icecream')
->findOneBy(array('_id' => (string)$answerId));
返回
doctrine.INFO: MongoDB query: {"find":true,"query":{"_id":{"$id":"52ced410e4b0fcc3da3a0c8b"}},"fields":[],"db":"myIcecreamDb","collection":"icecream"} [] []
有人知道嗎?
如果您有自己的字符串 _id(非 MongoId 對象),您可能需要使用 Id 的“策略”屬性。
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** @ODM\Document */
class MyPersistentClass
{
/** @ODM\Id(strategy="NONE") */
private $id;
public function setId($id) {
$this->id = $id;
}
}
您可以在此處的文檔中看到更多選項: http : //doctrine-mongodb-odm.readthedocs.org/en/latest/reference/basic-mapping.html#basic-mapping-identifiers
好的,我通過以下映射解決了這個問題
/**
* @MongoDB\Field(type="string",name="_id")
*/
protected $idstring;
/**
* @MongoDB\Id
*/
protected $id2;
那么 idstring 是字符串表示
我剛遇到這個。 似乎 Doctrine 緩存了一些映射。 我剛剛從常規的 MongoId 切換到字符串 id,每當我運行查詢時,Doctrine 都會嘗試將我的字符串 id 轉換為 MongoID(如您所描述的)。 這導致沒有找到任何結果。
我清除了所有緩存並修復了它 - Doctrine 現在只使用常規字符串 ID。
我正在使用以下內容:
/**
* @MongoDB\Id(strategy="NONE", type="string")
*/
private $id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.