简体   繁体   English

教义 odm,找到 id 不是对象 id 的文档

[英]doctrine odm, find document where id is not object id

In my mongodb I have a collection, where the id-field is not an object-Id I don't know why it was built like that and I can't change it from scratch cause there's probably a lot of software already using this pattern.在我的 mongodb 中,我有一个集合,其中 id-field 不是 object-Id 我不知道为什么它是这样构建的,我无法从头开始更改它,因为可能有很多软件已经在使用这种模式.

However, this is how such Icecream document looks like:但是,这就是 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) 
}

and with mongoshell I can find it like:使用 mongoshell 我可以找到它:

 db.icecream.find({"_id":"52d0283ae4b01db941dd763b"})

instead of using:而不是使用:

 db.icecream.find({"_id":ObjectId("52d0283ae4b01db941dd763b")})

so I tried a lot of queries to find it, but doctrine odm always does not the correct query, here's my latest try:所以我尝试了很多查询来找到它,但学说 odm 总是没有正确的查询,这是我的最新尝试:

  return $this->mongo->getManager()
    ->getRepository('DocumentBundle:Icecream')
    ->findOneBy(array('_id' => (string)$answerId));

which returns返回

   doctrine.INFO: MongoDB query: {"find":true,"query":{"_id":{"$id":"52ced410e4b0fcc3da3a0c8b"}},"fields":[],"db":"myIcecreamDb","collection":"icecream"} [] []

anybody any idea?有人知道吗?

If you have your own string _id (a NOT MongoId object), you may want to use the 'strategy' proprerty of Id.如果您有自己的字符串 _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;
    }    
}

You can see more options in the docs here: http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/basic-mapping.html#basic-mapping-identifiers您可以在此处的文档中看到更多选项: http : //doctrine-mongodb-odm.readthedocs.org/en/latest/reference/basic-mapping.html#basic-mapping-identifiers

okay i got this solved with the following mapping好的,我通过以下映射解决了这个问题

/**
 * @MongoDB\Field(type="string",name="_id")
 */
protected $idstring;

/**
 * @MongoDB\Id
 */
protected $id2;

then idstring is the string representation那么 idstring 是字符串表示

I just ran into this.我刚遇到这个。 It seems that Doctrine caches some of the mappings.似乎 Doctrine 缓存了一些映射。 I had just switched from a regular MongoId to a string id, Doctrine kept trying to convert my string id to a MongoID whenever I ran a query (as you described).我刚刚从常规的 MongoId 切换到字符串 id,每当我运行查询时,Doctrine 都会尝试将我的字符串 id 转换为 MongoID(如您所描述的)。 This resulted in no results found.这导致没有找到任何结果。

I cleared all of my caches and that fixed it - Doctrine now just uses the regular string id.我清除了所有缓存并修复了它 - Doctrine 现在只使用常规字符串 ID。

I'm using the following:我正在使用以下内容:

/**
 * @MongoDB\Id(strategy="NONE", type="string")
 */
private $id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM