简体   繁体   English

Doctrine MongoDB ODM - 使用查询构建器仅返回匹配的嵌入文档

[英]Doctrine MongoDB ODM - Return only matched embedded document using query builder

I am having trouble in returning only the matched embedded document using MongoDB ODM query builder in PHP.我在 PHP 中使用 MongoDB ODM 查询构建器仅返回匹配的嵌入文档时遇到问题。 Each embedded document has a MongoID generated at the time of creation.每个嵌入的文档都有一个在创建时生成的 MongoID。 Following is my document structure of collection Project :以下是我收集项目的文档结构:

{ 
    "_id" : ObjectId("59f889e46803fa3713454b5d"), 
    "projectName" : "usecase-updated", 
    "classes" : [
        {
            "_id" : ObjectId("59f9d7776803faea30b895dd"), 
            "className" : "OLA"
        }, 
        {
            "_id" : ObjectId("59f9d8ad6803fa4012b895df"), 
            "className" : "HELP"
        }, 
        {
            "_id" : ObjectId("59f9d9086803fa4112b895de"), 
            "className" : "DOC"
        }, 
        {
            "_id" : ObjectId("59f9d9186803fa4212b895de"), 
            "className" : "INVOC"
        }
    ]
}

Now i want to retrieve from the database only the class from the classes embedded documents which meets my criteria (ie class with a specific id) .This is how i am building the query:现在,我想从数据库中检索只能从嵌入文档符合我的标准(即与特定ID)。这就是我如何构建查询:

$qb = $dm->createQueryBuilder('Documents\Project');
$projectObj = $qb
    ->field('id')->equals("59f889e46803fa3713454b5d")
    ->field('classes')->elemMatch(
        $qb->expr()->field("id")->equals(new \MongoId("59f9d7776803faea30b895dd"))
    )
    ->hydrate(false)
    ->getQuery()
    ->getSingleResult();

First i match with the project id then i match with the embedded document class id.首先,我与项目 id 匹配,然后与嵌入的文档类 id 匹配。 I was expecting it to return only the embedded document of OLA like this:我期望它只返回OLA的嵌入文档,如下所示:

{ 
   "_id" : ObjectId("59f889e46803fa3713454b5d"), 
    "projectName" : "usecase-updated", 
    "classes" : [
          {
            "_id" : ObjectId("59f9d7776803faea30b895dd"), 
            "className" : "OLA"
          }
     ]
}

But doctrine is returning the whole Project record (shown in the start of question).I also tried with aggregation query building with the $match aggregation still the results are same the query i created with aggregation builder is as follows:但是学说正在返回整个项目记录(显示在问题的开头)。我还尝试使用$match聚合构建聚合查询,结果仍然相同,我使用聚合构建器创建的查询如下:

$qb = $dm->createAggregationBuilder('Documents\Project');
    $projectObj = $qb
        ->match()
        ->field('id')->equals($projectId)
        ->field('classes._id')->equals(new \MongoId($classId))
        ->execute()
        ->getSingleResult();

Can someone help me with regards to this issue?有人可以帮助我解决这个问题吗? How can i build query that i get the desired result as mentioned above.如上所述,我如何构建查询以获得所需的结果。

So after some trouble i am able to retrieve only the matched embedded document with the aggregation builder.因此,经过一些麻烦后,我只能使用聚合构建器检索匹配的嵌入文档。 So thanks to @ Alex Bles comment i was able to think more on using aggregation functions and i found $filter array aggregation function and tried building query using that and the final query was like this:所以感谢@ Alex Bles评论,我能够更多地考虑使用聚合函数,我找到了$filter数组聚合函数并尝试使用它构建查询,最终查询是这样的:

    $qb = $dm->createAggregationBuilder('Documents\Project');
    $classObject = $qb->match()
       ->field("id", new \MongoId($projectId))
       ->project()
       ->field("classes")
       ->filter('$classes', 'class', $qb->expr()->eq('$$class._id', new \MongoId($classId)))
       ->execute()->getSingleResult();

So in the query first i matched with the _id of the project.所以在查询中我首先匹配了项目的_id then within that project i projected the results for the classes using the $filter array aggregation method.然后在该项目中,我使用$filter数组聚合方法投影了classes的结果。 In the end i was able to get the embedded document filtered by their _id .最后,我能够获得由他们的_id过滤的嵌入文档。 Hope this will help someone in the future with same problem.希望这会帮助将来遇到同样问题的人。

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

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