dongyi7041 2012-12-12 23:55
浏览 219
已采纳

MongoDB ODM如何使用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.

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. 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. 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? Knowing that there can be multiple documents with the same location but a different pos value. And on top of that, have all the fields for the selected record that I have listed above in the ->select()

  • 写回答

2条回答 默认 最新

  • dongxiee0744 2012-12-13 08:27
    关注

    Instead of grouping with Map Reduce you can group with:

    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:

    So how would I create a query to get the highest numerical value in the pos field for each specific location

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题