紧接着上篇来,这篇主要讲,mongodb的group功能,做的还是挺强大的,相当对于find(),skip(),distinct()等,用法比较复杂。

测试数据

  1. > db.fruit.find();
  2. { "_id" : 1, "category" : "fruit", "name" : "apple" }
  3. { "_id" : 2, "category" : "fruit", "name" : "peach" }
  4. { "_id" : 3, "category" : "fruit", "name" : "banana" }
  5. { "_id" : 4, "category" : "veggie", "name" : "corn" }
  6. { "_id" : 5, "category" : "veggie", "name" : "broccoli" }

1,根据category分组

  1. > db.fruit.group(
  2. {
  3. key: { category: 1},
  4. reduce: function(obj, prev) {
  5. prev.items.push(obj.name);
  6. },
  7. initial: { items : [] }
  8. }
  9. );
  10. [
  11. {
  12. "category" : "fruit",
  13. "items" : [
  14. "apple",
  15. "peach",
  16. "banana"
  17. ]
  18. },
  19. {
  20. "category" : "veggie",
  21. "items" : [
  22. "corn",
  23. "broccoli"
  24. ]
  25. }
  26. ]

php代码如下

  1. $keys = array("category" => 1);
  2. $initial = array("items" => array());
  3. $reduce = "function (obj, prev) { prev.items.push(obj.name); }";
  4. $g = $collection->group($keys, $initial, $reduce);
  5. print_r($g);   //结果如下。
  6. Array
  7. (
  8. [retval] => Array
  9. (
  10. [0] => Array
  11. (
  12. [category] => fruit
  13. [items] => Array
  14. (
  15. [0] => apple
  16. [1] => peach
  17. [2] => banana
  18. )
  19. )
  20. [1] => Array
  21. (
  22. [category] => veggie
  23. [items] => Array
  24. (
  25. [0] => corn
  26. [1] => broccoli
  27. )
  28. )
  29. )
  30. [count] => 5
  31. [keys] => 2
  32. [ok] => 1
  33. )

2,根据category来分组,并统计count

  1. > db.fruit.group(
  2. {
  3. key: { category: 1},
  4. cond: { _id: { $gt: 2 } },
  5. reduce: function(obj, prev) {
  6. prev.items.push(obj.name);
  7. prev.count++;
  8. },
  9. initial: { items : [] ,count:0}
  10. }
  11. );
  12. [
  13. {
  14. "category" : "fruit",
  15. "items" : [
  16. "banana"
  17. ],
  18. "count" : 1
  19. },
  20. {
  21. "category" : "veggie",
  22. "items" : [
  23. "corn",
  24. "broccoli"
  25. ],
  26. "count" : 2
  27. }
  28. ]

php代码如下:

  1. $keys = array("category" => 1);
  2. $initial = array("items" => array(),'count'=>0);
  3. $reduce = "function (obj, prev) { " .
  4. "prev.items.push(obj.name); " .
  5. "prev.count++;" .
  6. "}";
  7. $condition = array('condition' => array("_id" => array( '$gt' => 2)));
  8. $g = $collection->group($keys, $initial, $reduce, $condition);
  9. print_r($g);   //结果如下。
  10. Array
  11. (
  12. [retval] => Array
  13. (
  14. [0] => Array
  15. (
  16. [category] => fruit
  17. [items] => Array
  18. (
  19. [0] => banana
  20. )
  21. [count] => 1
  22. )
  23. [1] => Array
  24. (
  25. [category] => veggie
  26. [items] => Array
  27. (
  28. [0] => corn
  29. [1] => broccoli
  30. )
  31. [count] => 2
  32. )
  33. )
  34. [count] => 3
  35. [keys] => 2
  36. [ok] => 1
  37. )

3,利用aggregate group功能,也挺强大

  1. > db.fruit.aggregate([
  2. { $match: { _id: {$gt:0} } },
  3. { $group: { _id: "$category", count: { $sum: 1 } } },
  4. { $sort: { count: -1 } }
  5. ]);
  6. { "_id" : "fruit", "count" : 3 }
  7. { "_id" : "veggie", "count" : 2 }

php代码如下:

  1. $cond = array(
  2. array(
  3. '$match' => array('_id' => array('$gt' => 0)),
  4. ),
  5. array(
  6. '$group' => array(
  7. '_id' => '$category',
  8. 'count' => array('$sum' => 1),
  9. ),
  10. ),
  11. array(
  12. '$sort' => array("count" => -1),
  13. ),
  14. );
  15. $result = $collection->aggregate($cond);
  16. print_r($result);    //结果如下:
  17. Array
  18. (
  19. [result] => Array
  20. (
  21. [0] => Array
  22. (
  23. [_id] => fruit
  24. [count] => 3
  25. )
  26. [1] => Array
  27. (
  28. [_id] => veggie
  29. [count] => 2
  30. )
  31. )
  32. [ok] => 1
  33. )

mongodb 的select 操作有很多,在这里,只是说了一些常用的功能。

mongodb group php 操作的更多相关文章

  1. 【翻译】MongoDB指南/CRUD操作(二)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...

  2. MongoDB基本命令行操作

    1. 连接MongoDB: Mongodb://username:password@hostname/dbname 2. 创建数据库: use dbname:如果数据库不存在则创建数据库,否则切换到指 ...

  3. Yii2的mongodb的聚合操作

    最近项目使用到mongodb的聚合操作,但是yii文档中对这方面资料较少,记录下 $where['created_time'] = ['$gt' => "$start_date_str ...

  4. springboot 学习之路 14(整合mongodb的Api操作)

    springboot整合mongodb: mongodb的安装和权限配置  请点击连接参考 mongodb集成 : 第一步:引如pom文件 第二步:配置文件配置mongodb路径: 第三步:关于mon ...

  5. MongoDB的聚合操作以及与Python的交互

    上一篇主要介绍了MongoDB的基本操作,包括创建.插入.保存.更新和查询等,链接为MongoDB基本操作. 在本文中主要介绍MongoDB的聚合以及与Python的交互. MongoDB聚合 什么是 ...

  6. MongoDB 的聚集操作

    聚合引言 聚集操作就是出来数据记录并返回计算结果的操作.MongoDB提供了丰富的聚集操作.可以检測和执行数据集上的计算.执行在mongod上的数据聚集简化了代码和资源限制. 像查询一样,在Mongo ...

  7. Java对MongoDB进行分组操作并统计各个分组的数量

    最近在检索MongoDB的数据时需要用到分组操作,由于没有现成的说明文档可参考,只能是在代码中不断调试.摸索前进:目前已现实了Java对MongoDB的分组操作,并统计各个分组的数量.现通过示例详细解 ...

  8. 【翻译】MongoDB指南/CRUD操作(一)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(一) 主要内容:CRUD操作简介,插入文档,查询文档. CRUD操作包括创建.读取.更新和删 ...

  9. MongoDB的CRUD操作

    1. 前言 在上一篇文章中,我们介绍了MongoDB.现在,我们来看下如何在MongoDB中进行常规的CRUD操作.毕竟,作为一个存储系统,它的基本功能就是对数据进行增删改查操作. MongoDB中的 ...

随机推荐

  1. Vivado使用技巧(二):封装自己设计的IP核

    由 judyzhong 于 星期五, 09/08/2017 - 14:58 发表 概述   Vivado在设计时可以感觉到一种趋势,它鼓励用IP核的方式进行设计.“IP Integrator”提供了原 ...

  2. IT人都非常忙(茫)

    我发现.身边的盆友都非常忙,要么在加班.要么加班刚回家:要么在出差,要么刚出差回来. 难道搞IT的人都非常忙么?忙还是茫? 大学期间.不知道未来要干什么.非常多人也不清楚应该学习哪些知识和技能.是否须 ...

  3. Sublime Text 3 快捷鍵

    /*On OS X, basic text manipulations (left, right, command+left, etc) make use of the system key bind ...

  4. Disk performance

    http://blogs.msdn.com/b/ntdebugging/archive/2014/12/09/disk-performance-internals.aspx http://blogs. ...

  5. C#中一道关于线程同步的练习题——模拟多窗口售票

    题目:模拟窗口卖票,四个窗口同时对外开放售票,需要按顺序售出. 要求:输出每一张票的售出时间和售出窗口,不能出现票未售出或者被售出多次的情况. using System; using System.C ...

  6. iosxcode7以后免证书真机测试方法如下

    步骤比较简单,我就简单总结一下. 1. 进入xcode,菜单栏选择xcode –> preferences (快捷键 command + ,)在Accounts选项卡添加自己的Apple ID ...

  7. matlab与VC6.0混合编程设置

    版本matlab 2009 和vc++6.0 SP6 步骤 1)  配置环境,新建一个VC工程,然后在VC界面的“工具->选项”的目录选项卡中的“include”中加入如下路径: 2)  D:\ ...

  8. Curator入门教程1

     简介 Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理 ...

  9. JavaScript 闭包原理分析

    本文转载至 http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html 另一篇很好的资料 http://www.k ...

  10. TP5 display()

    tp3.x $this->display(); tp5 return $this->fetch();