简体   繁体   English

MongoDB ODM如何使用map / reduce按特定字段分组?

[英]MongoDB ODM How do I group by a certain field with map/reduce?

I have a collection of documents that I want to query by a certain field called location , sort them by a pos field, and only return the highest value for each pos field. 我有一个文档集合,我想通过一个称为location的特定字段来查询文档,并按pos字段对其进行排序,并且仅返回每个pos字段的最大值。

So the query right now looks something like this: 因此查询现在看起来像这样:

$locations = array(1,2,3,4,5,6);

   $results = $this->dm->createQueryBuilder('\App\Document\Test')
        ->select('id', 'word', 'pos','change', 'title', 'coll_at', 'location')
        ->field('location')->in($locations)
        ->sort('location', 'asc')
        ->sort('pos', 'asc')
        ->getQuery()->execute();

But because there can be multiple entries for a specific location each with different pos , I then have to create a foreach loop to manipulate the data afterwards. 但是由于特定location可能有多个条目,每个条目具有不同的pos ,所以我必须创建一个foreach循环来随后处理数据。 In this scenario, I could take that shortcut just altering the data after it's returned, but I have other scenarios where it isn't efficient at all to do that. 在这种情况下,我可以采用快捷方式,只需在返回数据后更改数据,但是在其他情况下,这样做根本无效。 So I created this smaller scenario to try and figure out how to either use Doctrine ODM's group query, or even map & reduce it. 因此,我创建了这个较小的方案,以尝试弄清楚如何使用Doctrine ODM的group查询,甚至映射并简化它。 Not sure the best way to. 不确定最好的方法。 I see lots of examples of getting a running total, etc. 我看到了很多获得总计的示例,等等。

So how would I create a query to get the highest numerical value in the pos field for each specific location ? 那么,我将如何创建查询以在pos字段中获取每个特定location的最高数值? Knowing that there can be multiple documents with the same location but a different pos value. 知道可能存在多个具有相同locationpos值不同的文档。 And on top of that, have all the fields for the selected record that I have listed above in the ->select() 最重要的是,在->select()列出上面列出的所有选定记录的字段

Instead of grouping with Map Reduce you can group with: 除了使用Map Reduce分组外,您还可以分组:

Do note that I believe the default implementation of Doctrines distinct actually works on the old group() command for MongoDB which is basically an MR wrapped however: 请注意,我相信Doctrines的默认实现实际上可用于MongoDB的旧group()命令,该命令基本上是MR包装的:

So how would I create a query to get the highest numerical value in the pos field for each specific location 因此,我将如何创建查询以在pos字段中获取每个特定位置的最高数值

That might work, however if not then you will need to use the aggregation framework, however in Doctrine this is a little harder since they don't seem to have a true helper that links into the rest of their framework for it: https://github.com/doctrine/DoctrineMongoDBBundle/issues/165 so you must the command function to run it. 那可能行得通,但是如果没有,那么您将需要使用聚合框架,但是在Doctrine中,这有点困难,因为他们似乎没有真正的助手可以链接到框架的其余部分: https:/ /github.com/doctrine/DoctrineMongoDBBundle/issues/165,因此您必须使用command功能才能运行它。

Edit 编辑

I am a bit of a Doctrine noob myself so this is taken from examples: 我本人有点教条,所以这是从示例中得出的:

$connection = $this->get('doctrine_mongodb')->getConnection();
$mongo = $connection->getMongo();
if(!$mongo){
    $connection->connect();
    $mongo = $connection->getMongo();
}
$db = $mongo->selectDB('test_database');
$aggregate_results = $db ->command(array( 
            "aggregate" => "my_collection",
            "pipeline" => 
                array( 
                    array( '$group' => array( 'location' => '$location', 'pos' => array('$sum' => '$pos'))),
                    array( '$sort' => array( "pos" =>1 ) )
                )
        ));

This will run the aggregation for you, it won't be exactly what your looking for I don't think but play around with it. 这将为您运行聚合,这不是您要找的,不是我想的,而是尝试使用它。

In addition to Sammaye's answer. 除了Sammaye的答案。 Doctrine ODM does support group queries: Doctrine ODM确实支持组查询:

http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/query-builder-api.html#group-queries http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/query-builder-api.html#group-queries

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

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