聚合函数

- 添加基础数据:
db.local.save({contry:'中国',name:'小明',score:77});
db.local.save({contry:'中国',name:'小红',score:88});
db.local.save({contry:'中国',name:'小张',score:99});
db.local.save({contry:'美国',name:'jack',score:45});
db.local.save({contry:'美国',name:'rose',score:67});
db.local.save({contry:'美国',name:'mick',score:89});

- 需要求当前集合的记录数:
- `db.local.find().count();`


- 求最大值
-求整个集合的总成绩
+ db.集合名.聚合({ 组的划分规则{_id:'1',显示内容:{$sum:'$score'}} })
- 求所有人的平均分
- `db.local.aggregate({$group:{_id:'$contry',sumscore:{$avg:'$score' } }});`

- 求按国家分组,求所有国家的总分
- `db.users.aggregate({$group:{_id:'$contry',sumScore:{$sum:'$score'}}});`

联合查询

db.orders.insert([
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
{ "_id" : 3 }
]);
db.inventory.insert([
{ "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 },
{ "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 },
{ "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 },
{ "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 },
{ "_id" : 5, "sku": null, description: "Incomplete" },
{ "_id" : 6 }
]);
db.orders.aggregate([
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
]);
```

效果:

> show  dbs;
admin .000GB
config .000GB
local .000GB
mini_db .001GB
> db.mini_db.insert([
... { "_id" : , "item" : "almonds", "price" : , "quantity" : },
... { "_id" : , "item" : "pecans", "price" : , "quantity" : },
... { "_id" : }
... ]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : ,
"nUpserted" : ,
"nMatched" : ,
"nModified" : ,
"nRemoved" : ,
"upserted" : [ ]
})
> db.config.insert([
... { "_id" : , "sku" : "almonds", description: "product 1", "instock" : },
... { "_id" : , "sku" : "bread", description: "product 2", "instock" : },
... { "_id" : , "sku" : "cashews", description: "product 3", "instock" : },
... { "_id" : , "sku" : "pecans", description: "product 4", "instock" : },
... { "_id" : , "sku": null, description: "Incomplete" },
... { "_id" : }
... ]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : ,
"nUpserted" : ,
"nMatched" : ,
"nModified" : ,
"nRemoved" : ,
"upserted" : [ ]
})
> db.mini_db.aggregate([
... {
... $lookup:
... {
... from: "config",
... localField: "item",
... foreignField: "sku",
... as: "config_docs"
... }
... }
... ]);
{ "_id" : , "item" : "almonds", "price" : , "quantity" : , "config_docs" : [ { "_id" : , "sku" : "almonds", "description" : "product 1", "instock" : } ] }
{ "_id" : , "item" : "pecans", "price" : , "quantity" : , "config_docs" : [ { "_id" : , "sku" : "pecans", "description" : "product 4", "instock" : } ] }
{ "_id" : , "config_docs" : [ { "_id" : , "sku" : null, "description" : "Incomplete" }, { "_id" : } ] }
>

mongodb的学习 (3)的更多相关文章

  1. NoSQL之【MongoDB】学习(三):配置文件说明

    摘要: 继上一篇NoSQL之[MongoDB]学习(一):安装说明 之后,知道了如何安装和启动MongoDB,现在对启动时指定的配置文件(mongodb.conf)进行说明,详情请见官方. 启动Mon ...

  2. [转载]MongoDB开发学习(2)索引的基本操作

    索引能够极大的提高查询的效率.在数据库中简历索引必不可少. 在MongoDB中可以很轻松的创建索引. 默认索引_id_ 开启MongoDB服务器,创建数据库cnblogs,创建集合Users .(关于 ...

  3. 双刃剑MongoDB的学习和避坑

    双刃剑MongoDB的学习和避坑 MongoDB 是一把双刃剑,它对数据结构的要求并不高.数据通过key-value的形式存储,而value的值可以是字符串,也可以是文档.所以我们在使用的过程中非常方 ...

  4. MongoDB的学习--索引类型和属性(转)

    原文链接:MongoDB的学习--索引类型和属性 索引类型 MongDB的索引分为以下几种类型:单键索引.复合索引.多键索引.地理空间索引.全文本索引和哈希索引 单键索引(Single Field I ...

  5. mongodb数据库学习【安装及简单增删改查】

    //@desn:mongodb数据库学习 //@desn:码字不宜,转载请注明出处 //@author:张慧源  <turing_zhy@163.com> //@date:2018/08/ ...

  6. Mongodb数据库学习系列————(一)Mongodb数据库主从复制的搭建

    Mongodb数据库主从复制的搭建 Writeby:lipeng                                    date:2014-10-22 最近项目上用到了位置查询,在网上 ...

  7. mongodb入门学习小记

    Mongodb 简单入门(个人学习小记) 1.安装并注册成服务:(示例) E:\DevTools\mongodb3.2.6\bin>mongod.exe --bind_ip 127.0.0.1 ...

  8. MongoDB快速学习笔记

    一,下载. XP系统,32位的下载地址: https://www.mongodb.org/dl/win32/i386 例:win32/mongodb-win32-i386-2.0.7.ziphttp: ...

  9. MongoDB的学习--文档的查询

    继续关于<MongoDB权威指南>记录,今天的内容是文档的查询~~ MongoDB官网地址:http://www.mongodb.org/ 我使用的是MongoDB 2.4.8 find函 ...

  10. MongoDB入门学习(一)—— 安装和启动

    最近由于工作需要,开始学习MongoDB数据库了.第一篇博文就从这里开始吧,以此记录下学习中的点点滴滴,为自己加油呢! (一) MongoDB简介 网上搜搜了一下:(来源:http://www.run ...

随机推荐

  1. java应用之solr入门篇

    前言 solr是apache项目的一款全文搜索应用. 官方文档http://lucene.apache.org/solr/guide/6_6/ 入门流程 1.安装   --->  2.启动  - ...

  2. Node.js定时邮件的那些事儿

    近开发一个项目,需要在Node.js程序里实现定期给管理员发邮件的功能. 笔者平时只会在Web界面收发邮件.对邮件的原理完全不懂(可能大学教过,然而全忘了),直到要解决这个问题.请教了几个业务的同事, ...

  3. PHP浮点精度问题

    使用php+ - * /计算浮点数的时候,可能会遇到一些计算结果错误的问题,如下: <?php echo intval(0.58 * 100); //输出57 解决办法 <?php ech ...

  4. pipeline语法之判断一个文件存在与否

    先看一个例子,原理,根据命令返回的状态值来判断它的存在与否 pipeline { agent any stages { stage("check file if exists"){ ...

  5. 【leetcode】662. Maximum Width of Binary Tree

    题目如下: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...

  6. epoll学习(二)

    首先看程序一,这个程序想要实现的功能是当用户从控制台有任何输入操作时,输出”hello world!”. l 程序一 #include <unistd.h> #include <io ...

  7. Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo dp+矩阵快速幂

    E. Okabe and El Psy Kongroo   Okabe likes to take walks but knows that spies from the Organization c ...

  8. Alpha冲刺阶段博客目录

    Alpha冲刺阶段博客目录 Scrum Meeting 时间 链接 内容 第六周 https://www.cnblogs.com/error0/p/11815255.html 需求分析 第七周 htt ...

  9. Linux系统之-介绍,主要特性

    Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协议.它支持32位 ...

  10. Django中的HttpRequsest 和Httpresponse对象

    HttpRequest对象:每一个用户请求在到达视图函数的同时,django会自动创建一个HttpRequest对象并把这个对象当做第一个参数传给要调用的views方法,HttpRequest对象里封 ...