mongodb的学习 (3)
聚合函数
- 添加基础数据:
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)的更多相关文章
- NoSQL之【MongoDB】学习(三):配置文件说明
摘要: 继上一篇NoSQL之[MongoDB]学习(一):安装说明 之后,知道了如何安装和启动MongoDB,现在对启动时指定的配置文件(mongodb.conf)进行说明,详情请见官方. 启动Mon ...
- [转载]MongoDB开发学习(2)索引的基本操作
索引能够极大的提高查询的效率.在数据库中简历索引必不可少. 在MongoDB中可以很轻松的创建索引. 默认索引_id_ 开启MongoDB服务器,创建数据库cnblogs,创建集合Users .(关于 ...
- 双刃剑MongoDB的学习和避坑
双刃剑MongoDB的学习和避坑 MongoDB 是一把双刃剑,它对数据结构的要求并不高.数据通过key-value的形式存储,而value的值可以是字符串,也可以是文档.所以我们在使用的过程中非常方 ...
- MongoDB的学习--索引类型和属性(转)
原文链接:MongoDB的学习--索引类型和属性 索引类型 MongDB的索引分为以下几种类型:单键索引.复合索引.多键索引.地理空间索引.全文本索引和哈希索引 单键索引(Single Field I ...
- mongodb数据库学习【安装及简单增删改查】
//@desn:mongodb数据库学习 //@desn:码字不宜,转载请注明出处 //@author:张慧源 <turing_zhy@163.com> //@date:2018/08/ ...
- Mongodb数据库学习系列————(一)Mongodb数据库主从复制的搭建
Mongodb数据库主从复制的搭建 Writeby:lipeng date:2014-10-22 最近项目上用到了位置查询,在网上 ...
- mongodb入门学习小记
Mongodb 简单入门(个人学习小记) 1.安装并注册成服务:(示例) E:\DevTools\mongodb3.2.6\bin>mongod.exe --bind_ip 127.0.0.1 ...
- MongoDB快速学习笔记
一,下载. XP系统,32位的下载地址: https://www.mongodb.org/dl/win32/i386 例:win32/mongodb-win32-i386-2.0.7.ziphttp: ...
- MongoDB的学习--文档的查询
继续关于<MongoDB权威指南>记录,今天的内容是文档的查询~~ MongoDB官网地址:http://www.mongodb.org/ 我使用的是MongoDB 2.4.8 find函 ...
- MongoDB入门学习(一)—— 安装和启动
最近由于工作需要,开始学习MongoDB数据库了.第一篇博文就从这里开始吧,以此记录下学习中的点点滴滴,为自己加油呢! (一) MongoDB简介 网上搜搜了一下:(来源:http://www.run ...
随机推荐
- SVN 的安装及配置
1.安装subversion yum install subversion 2.创建仓库目录 mkdir /svn 3. 为项目3创建仓库 svnadmin create /svn/proj3 4.修 ...
- JS的一些日常
1. [1] == 1 => true; 很神奇.. 2.js变量命名规则: // 1.变量命名必须以字母.下划线”_”或者”$”为开头.其他字符可以是字母._.美元符号或数字. / ...
- pod的状态分析
Pod状态 状态 描述 Running 该 Pod 已经绑定到了一个节点上,Pod 中所有的容器都已被创建.至少有一个容器正在运行,或者正处于启动或重启状态. Pending Pod 已被 Kuber ...
- sql-主键、外键、索引
SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...
- 07-图5 Saving James Bond - Hard Version(30 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 芒果学院Python高级开发课程
- ZROI week6
ZROI week6 T1 用一个类似背包的东西记录答案. T2 好像直接用|操作即可. T3 瞎搞就完事了 T4 启发式合并,然而变量写错了,就没了... 总结 100 + 100 + 100 + ...
- 探索Redis设计与实现1:Redis 的基础数据结构概览
本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...
- python的OS模块生成100个txt文件
#!/user/bin/env/python35 # -*-coding:utf-8-*- # author:Keekuun """ 问题:生成一个文件夹,文件夹下面生成 ...
- GIL - global interpreter lock
python是一个解释型语言,但是可以使用多个解释器.比如C++,但是可以用不同的编译器来编译成可执行代码.有名的编译器例如GCC,INTEL C++,Visual C++等.Python也一样,同样 ...