[英]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.