mongodb group php 操作
紧接着上篇来,这篇主要讲,mongodb的group功能,做的还是挺强大的,相当对于find(),skip(),distinct()等,用法比较复杂。
测试数据
- > db.fruit.find();
- { "_id" : 1, "category" : "fruit", "name" : "apple" }
- { "_id" : 2, "category" : "fruit", "name" : "peach" }
- { "_id" : 3, "category" : "fruit", "name" : "banana" }
- { "_id" : 4, "category" : "veggie", "name" : "corn" }
- { "_id" : 5, "category" : "veggie", "name" : "broccoli" }
1,根据category分组
- > db.fruit.group(
- {
- key: { category: 1},
- reduce: function(obj, prev) {
- prev.items.push(obj.name);
- },
- initial: { items : [] }
- }
- );
- [
- {
- "category" : "fruit",
- "items" : [
- "apple",
- "peach",
- "banana"
- ]
- },
- {
- "category" : "veggie",
- "items" : [
- "corn",
- "broccoli"
- ]
- }
- ]
php代码如下
- $keys = array("category" => 1);
- $initial = array("items" => array());
- $reduce = "function (obj, prev) { prev.items.push(obj.name); }";
- $g = $collection->group($keys, $initial, $reduce);
- print_r($g); //结果如下。
- Array
- (
- [retval] => Array
- (
- [0] => Array
- (
- [category] => fruit
- [items] => Array
- (
- [0] => apple
- [1] => peach
- [2] => banana
- )
- )
- [1] => Array
- (
- [category] => veggie
- [items] => Array
- (
- [0] => corn
- [1] => broccoli
- )
- )
- )
- [count] => 5
- [keys] => 2
- [ok] => 1
- )
2,根据category来分组,并统计count
- > db.fruit.group(
- {
- key: { category: 1},
- cond: { _id: { $gt: 2 } },
- reduce: function(obj, prev) {
- prev.items.push(obj.name);
- prev.count++;
- },
- initial: { items : [] ,count:0}
- }
- );
- [
- {
- "category" : "fruit",
- "items" : [
- "banana"
- ],
- "count" : 1
- },
- {
- "category" : "veggie",
- "items" : [
- "corn",
- "broccoli"
- ],
- "count" : 2
- }
- ]
php代码如下:
- $keys = array("category" => 1);
- $initial = array("items" => array(),'count'=>0);
- $reduce = "function (obj, prev) { " .
- "prev.items.push(obj.name); " .
- "prev.count++;" .
- "}";
- $condition = array('condition' => array("_id" => array( '$gt' => 2)));
- $g = $collection->group($keys, $initial, $reduce, $condition);
- print_r($g); //结果如下。
- Array
- (
- [retval] => Array
- (
- [0] => Array
- (
- [category] => fruit
- [items] => Array
- (
- [0] => banana
- )
- [count] => 1
- )
- [1] => Array
- (
- [category] => veggie
- [items] => Array
- (
- [0] => corn
- [1] => broccoli
- )
- [count] => 2
- )
- )
- [count] => 3
- [keys] => 2
- [ok] => 1
- )
3,利用aggregate group功能,也挺强大
- > db.fruit.aggregate([
- { $match: { _id: {$gt:0} } },
- { $group: { _id: "$category", count: { $sum: 1 } } },
- { $sort: { count: -1 } }
- ]);
- { "_id" : "fruit", "count" : 3 }
- { "_id" : "veggie", "count" : 2 }
php代码如下:
- $cond = array(
- array(
- '$match' => array('_id' => array('$gt' => 0)),
- ),
- array(
- '$group' => array(
- '_id' => '$category',
- 'count' => array('$sum' => 1),
- ),
- ),
- array(
- '$sort' => array("count" => -1),
- ),
- );
- $result = $collection->aggregate($cond);
- print_r($result); //结果如下:
- Array
- (
- [result] => Array
- (
- [0] => Array
- (
- [_id] => fruit
- [count] => 3
- )
- [1] => Array
- (
- [_id] => veggie
- [count] => 2
- )
- )
- [ok] => 1
- )
mongodb 的select 操作有很多,在这里,只是说了一些常用的功能。
mongodb group php 操作的更多相关文章
- 【翻译】MongoDB指南/CRUD操作(二)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...
- MongoDB基本命令行操作
1. 连接MongoDB: Mongodb://username:password@hostname/dbname 2. 创建数据库: use dbname:如果数据库不存在则创建数据库,否则切换到指 ...
- Yii2的mongodb的聚合操作
最近项目使用到mongodb的聚合操作,但是yii文档中对这方面资料较少,记录下 $where['created_time'] = ['$gt' => "$start_date_str ...
- springboot 学习之路 14(整合mongodb的Api操作)
springboot整合mongodb: mongodb的安装和权限配置 请点击连接参考 mongodb集成 : 第一步:引如pom文件 第二步:配置文件配置mongodb路径: 第三步:关于mon ...
- MongoDB的聚合操作以及与Python的交互
上一篇主要介绍了MongoDB的基本操作,包括创建.插入.保存.更新和查询等,链接为MongoDB基本操作. 在本文中主要介绍MongoDB的聚合以及与Python的交互. MongoDB聚合 什么是 ...
- MongoDB 的聚集操作
聚合引言 聚集操作就是出来数据记录并返回计算结果的操作.MongoDB提供了丰富的聚集操作.可以检測和执行数据集上的计算.执行在mongod上的数据聚集简化了代码和资源限制. 像查询一样,在Mongo ...
- Java对MongoDB进行分组操作并统计各个分组的数量
最近在检索MongoDB的数据时需要用到分组操作,由于没有现成的说明文档可参考,只能是在代码中不断调试.摸索前进:目前已现实了Java对MongoDB的分组操作,并统计各个分组的数量.现通过示例详细解 ...
- 【翻译】MongoDB指南/CRUD操作(一)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(一) 主要内容:CRUD操作简介,插入文档,查询文档. CRUD操作包括创建.读取.更新和删 ...
- MongoDB的CRUD操作
1. 前言 在上一篇文章中,我们介绍了MongoDB.现在,我们来看下如何在MongoDB中进行常规的CRUD操作.毕竟,作为一个存储系统,它的基本功能就是对数据进行增删改查操作. MongoDB中的 ...
随机推荐
- .NET Remoting中的通道注册
今天我的同事使用Remoting注册一个新通道.奇怪的是,通道始终无法注册,总是报告异常“该通道已被占用”.我明白这个异常出现的原因,但不明白的是此时系统并未使用任何一个通道,为何会有这个异常呢?即使 ...
- 【Android】20.2 视频播放
分类:C#.Android.VS2015: 创建日期:2016-03-11 一.简介 本节例子和上一节的音频播放例子相似,也是最简单的示例,比如并没有考虑视频播放过程中电话打入的情况,也没有考虑复杂的 ...
- Java项目(5)——单例模式的应用与研究
单例模式是非常别致的一个模式,非常少有人拿它跟其它模式相比,由于,单例模式非常easy,非常特别,作用就是保证一个类有唯一一个实例,并让一个全局变量使得它能被訪问.而保证这个类仅仅被实例化一次的办法就 ...
- ubuntu 14.04英文环境设置成中文
适用于ubuntu 14.04英文版的系统,其它版本的设置应该是大同小异的. 进入ubuntu系统,在顶部齿状标志找到system... 2.在personal找到Language Support 3 ...
- c++ 使用vs2010调用 win32api
以前读书时都是用vc6.0.后来学c#用vs.装系统只装了vs2010.今天用vs2010写c++程序.发现有点陌生.就总结下,免得以后忘记. 首先用vs2010选择c++语言.新建一个win32控制 ...
- vue2的全局变量的设置
最近在学习VUE.js 中间涉及到JS全局变量,与其说是VUE的全局变量,不如说是模块化JS开发的全局变量. 1.全局变量专用模块 就是以一个特定模块来组织管理这些全局量,需要引用的地方导入该模块便好 ...
- Linux Jenkins配置Git
1.卸载Centos自带的git1.7.1:通过git –version查看系统带的版本,Centos应该自带的是git版本是1.7.1 终端输入:yum remove git 2.安装所需软件包 终 ...
- gradle本地文件仓库 (52.6.5. Flat directory repository)
If you want to use a (flat) filesystem directory as a repository, simply type: Example 52.32. Flat r ...
- Gradle修改本地仓库的位置
http://blog.csdn.net/tower888/article/details/38879955 http://blog.csdn.net/z69183787/article/detail ...
- Windows server 2012公用网络修改为专用网络
普通环境路径如下: [控制面板]--[系统和安全]--[管理工具]--[本地安全策略]--[网络列表管理器策略]--[网络]--[网络位置],设定之后也可以设定一下[用户权限] 域控环境路径如下: 不 ...