简体   繁体   English

如何通过MongoDB在Doctrine ODM中引用文档进行查找?

[英]How to find by referenced document in Doctrine ODM with MongoDB?

I have one document in my "params" collection like this: 我的“params”集合中有一个文档,如下所示:

{
  "_id": ObjectId("4d124cef3ffcf6f410000037"),
  "code": "color",
  "productTypes": [
    {
      "$ref": "productTypes",
      "$id": ObjectId("4d120a2d2b8d8d3010000000"),
      "$db": "test"
    }
  ]
}

the referenced document is this: 引用的文件是这样的:

{
  "_id": ObjectId("4d120a2d2b8d8d3010000000"),
  "code": "car"
}

I'm using DoctrineODM to fetch the "param" documents which referenced "productType" is "car". 我正在使用DoctrineODM来获取引用“productType”为“car”的“param”文档。 I'm using this code: 我正在使用此代码:

$query = $dm->createQuery('Cms\Model\Param');
$query->field('productTypes.code')->equals('car');
$result = $query->execute();
var_dump($result);

but the result is an empty array. 但结果是一个空数组。 How can i do this? 我怎样才能做到这一点?

If you using ReferenceMany or ReferenceOne you can't query by any reference document field, except reference document id. 如果您使用ReferenceMany或ReferenceOne,则无法通过任何参考文档字段进行查询,除了参考文档ID。

If you need query on code from referenced collection you should use EmbedMany instead of ReferenceMany . 如果您需要查询引用集合中的code ,则应使用EmbedMany而不是ReferenceMany

In this case your document will be: 在这种情况下,您的文档将是:

{
  "_id": ObjectId("4d124cef3ffcf6f410000037"),
  "code": "color",
  "productTypes": [
     {
       "_id": ObjectId("4d120a2d2b8d8d3010000000"),
       "code": "car"
     }
  ]
}

And following query will work: 以下查询将起作用:

$query = $dm->createQuery('Cms\Model\Param');
$query->field('productTypes.code')->equals('car');
$result = $query->execute();
var_dump($result);

Also if your ProductType code is unique you can use it instead of MongoId , in this case you can query on $id: 此外,如果您的ProductType代码是唯一的,您可以使用它而不是MongoId ,在这种情况下,您可以查询$ id:

{
  "_id": ObjectId("4d124cef3ffcf6f410000037"),
  "code": "color",
  "productTypes": [
    {
      "$ref": "productTypes",
      "$id": 'car',
      "$db": "test"
    }
  ]
}

Referenced document: 参考文件:

{
  "_id": 'car'
}

Query: 查询:

$query->field('productTypes.$id')->equals('car');

You must use the references() method of Query Builder for a @MongoDB\\ReferenceOne like https://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/query-builder-api.html 您必须使用查询生成器的references()方法来访问@MongoDB \\ ReferenceOne,例如https://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/query-builder-api.html

$productType = $dm->getRepository('Cms\Model\ProductTypes')->findOneByCode('car');

$queryBuilder = $dm->getRepository('Cms\Model\Param')->createQueryBuilder()
                   ->field('productTypes')->references($productType);

$results = $queryBuilder->getQuery()->execute();


PS : use includesReferenceTo() a @MongoDB\\ReferenceMany PS :使用includesReferenceTo()一个@MongoDB \\ ReferenceMany

->field('productTypes.code')->equals(new \MongoRegex('/.*car.*/i'))

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

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