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中的 ...
随机推荐
- WCF入门学习3-配置文件与部署iis
配置文件设置 --------------------------------------------------- 创建的时候都会有个配置文件,其实有一个WCF配置编辑器,右键就可以点出来设置. 需 ...
- 【Codewars】7×7 摩天大楼
介绍 链接:7×7 Skyscrapers C#答案(原因:懒,但是完全可以转成C++):bajdcc/learnstl 题目(机翻): 在7乘7格的网格中,你只想在每个广场上放置一个摩天大楼,只有一 ...
- 三、为什么String在Java中是不可更改的
String在Java中是个不可更改的类.一个不可更改的类简单来说就是这个类的所有实例是不可以更改的.所有的实例信息在创建的时候被初始化而且信息是不可以更改的.不可更改的类有很多好处.这篇文章总结了为 ...
- 知乎:在卡内基梅隆大学 (Carnegie Mellon University) 就读是怎样一番体验?
转自:http://www.zhihu.com/question/24295398 知乎 Yu Zhang 知乎搜索 首页 话题 发现 消息 调查类问题名校就读体验修改 在卡内基梅隆大学 (Car ...
- .NET项目web自动化测试实战——Selenium 2.0
PS:这次用公司的项目来练手,希望公司不会起诉我,因为我绝对是抱着学习的态度,没有任何恶意.仅供交流学习. 该项目是基于SharePoint平台所开发的门户网站,为了切身感受一下Selenium 2. ...
- Python ORM框架SQLAlchemy学习笔记之数据添加和事务回滚介绍
1. 添加一个新对象 前面介绍了映射到实体表的映射类User,如果我们想将其持久化(Persist),那么就需要将这个由User类建立的对象实例添加到我们先前创建的Session会话实例中: 复制代码 ...
- c++重载前置++和--
C语言中,前置和后置++,--都不能作为左值,而在c++中,前置的++和--可以作为左值,从下面的重载运算符中也可以看出,它们返回的是引用,我不知道为什么这里和c语言中不同,但c++类似的提升还有三目 ...
- 使用HttpWebRequest调用wcf一段代码
public class HttpClass { internal static HttpWebRequest _httpWebRequest; public static void Request( ...
- kubernetes master 高可用一键部署
#地址见:https://github.com/SILLKY/kubernetes-pro/tree/master/Master-HA#包括其他一些文件,适当版本1.6.1#!/bin/bash ho ...
- SpringMVC 常用工具类与接口
ClassPathResource 在类路径下读取资源 public final String getPath()public boolean exists()public InputStream g ...