概述html
数据存储是为了可查询,统计。若数据只需存储,不须要查询,这种数据也没有多大价值git
本篇介绍Mongodbgithub
准备工做mongodb
准备10000条数据express
var orders = new Array(); for (var i = 10000; i < 20000; i++) { orders[i] = { orderNo: i + Math.random().toString().substr(3, 3), price: Math.round(Math.random() * 10000) / 100, qty: Math.floor(Math.random() * 10) + 1, orderTime: new Date(new Date().setSeconds(Math.floor(Math.random() * 10000))) }; } db.order.insert(orders);
聚合查询缓存
Mongodb的聚合函数操做都在db.collection.aggregate,经过定义聚合管道(一组规则),达到分组,统计等功能,下面介绍经常使用的几种聚合函数app
分组管道($group)less
格式dom
{ $group: { _id: <expression>, // Group By Expression <field1>: { <accumulator1> : <expression1> }, ... } }
_id 是分组字段,若指定_id = null 或常量字段,就是将整个结果集分组。ide
分组统计字段格式{ <accumulator1> : <expression1> }
累计器操做(Accumulator Operator)参考Accumulator Operator
假设如今须要统计天天每一个小时的订单总价格,平均价格,最大,最小,总订单数等
db.order.aggregate([ { $group: { //分组字段,这里用到$dateToString格式化,这里按小时统计 _id: { $dateToString: { format: "%Y-%m-%d %H", date: "$orderTime" } }, //总价格 totalPrice: { $sum: "$price" }, //分组第一个订单 firstOrder: { $first: "$orderNo" }, //分组最后一个订单 lastOrder: { $last: "$orderNo" }, //平均价格 averagePrice: { $avg: "$price" }, //最大价格 maxPrice: { $max: "$price" }, //最小价格 minPrice: { $min: "$price" }, //总订单数 totalOrders: { $sum: 1 }, } } ])
返回结果
{ "_id" : "2020-04-12 15", "totalPrice" : 172813.68, "firstOrder" : "10000263", "lastOrder" : "19999275", "averagePrice" : 49.20662870159453, "maxPrice" : 99.94, "minPrice" : 0.01, "totalOrders" : 3512 } { "_id" : "2020-04-12 13", "totalPrice" : 80943.98, "firstOrder" : "10004484", "lastOrder" : "19991554", "averagePrice" : 50.780414052697616, "maxPrice" : 99.81, "minPrice" : 0.08, "totalOrders" : 1594 } { "_id" : "2020-04-12 14", "totalPrice" : 181710.15, "firstOrder" : "10001745", "lastOrder" : "19998830", "averagePrice" : 49.76996713229252, "maxPrice" : 99.93, "minPrice" : 0.01, "totalOrders" : 3651 } { "_id" : "2020-04-12 16", "totalPrice" : 63356.12, "firstOrder" : "10002711", "lastOrder" : "19995793", "averagePrice" : 50.97032984714401, "maxPrice" : 99.95, "minPrice" : 0.01, "totalOrders" : 1243 }
筛选管道($match)
格式
{ $match: { <query> } }
这个比较简单,就是筛选数据
假设我如今须要筛选金额在(10,15)之间的
db.orders.aggregate([ { $match: { "price": { $gt: 10, $lt: 15 } } } ])
排序管道($sort)
格式
{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }
指定字段排序,1:升序,-1:倒序
限制条数($limit)
格式
{ $limit: <positive integer> }
Mongodb的聚合管道有不少,具体不一一列出,参考Aggregation Pipeline Stages — MongoDB Manual
带有(aggregation)就是均可以用于聚合管道
说了那么多,其实都没有使用Mongodb聚合函数最强大的功能,就是组合管道使用,查询咱们须要数据,由于Mongodb提供的聚合管道函数很是多,因此组合起来使用是很是强大。
值得注意是管道的顺序,Mongodb是按你定义的顺序,将每一步执行的结果集传给下一个管道处理,输出是最后一个管道的结果集,因此不一样的管道顺序会有可能获得不是预期的结果,甚至报错(这种状况报错甚至比获得不是预期的结果可能还好)db.order.aggregate([ { $match: { "price": { $gt: 10, $lt: 50 }, "qty": { $lte: 5 } } }, { $sort: { "price": -1 } }, { $skip: 50 }, { $sort: { "price": 1 } }, { $skip: 50 }, { $group: { _id: { $dateToString: { format: "%Y-%m-%d %H", date: "$orderTime" } }, totalPrice: { $sum: "$price" }, totalOrders: { $sum: 1 } } }, { $sort: { "totalPrice": 1 } } ])
解决思路
固定集合
概述
capped-collection are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.
从上面定义能够看出固定集合具备几个特性
根据固定集合特性,固定集合适合用于如下场景
固定集合限制
固定集合使用
1. 建立固定集合
db.createCollection("log", { capped : true, size : 4096, max : 5000 } )
字段 | 必须 | 说明 |
---|---|---|
capped | 是 | 是否建立固定集合 |
size | 是 | 固定集合大小,单位:字节 |
max | 否 | 文档数量大小限制 |
size 和 max 是或关系,超出其中一个限制都会覆盖旧文档
2. 检查集合是否固定集合
db.collection.isCapped()
3. 将一个非固定的集合转换固定集合
db.runCommand({"convertToCapped": "mycoll", size: 100000});
测试固定集合
1. 超过限制文档数
// 1. 建立固定集合,大小1M,最大文档数量10 db.createCollection("log", { capped: true, size: 1024 * 1024, max: 10 }); // 2. 插入200条数据 for (var i = 0; i < 200; i++) { db.log.insertOne({ "_id": i + 1, "userId": Math.floor(Math.random() * 1000), "content": "登陆" + ("0000" + i).slice(-4), "createTime": new Date(), }); }
再查询如今Mongodb存储状况
db.log.stats()
能够看出每一个对象都是占有78个字节,由于字段都是定长的
2. 验证操做存储大小
If the size field is less than or equal to 4096, then the collection will have a cap of 4096 bytes. Otherwise, MongoDB will raise the provided size to make it an integer multiple of 256.
若是size的字段设置小于4096,Mongodb将会提供一个256的倍数的数据存储大小
假设256的大小,256 / 78 = 3.282051282051282,应该能存3个文档
// 1. 删除以前固定集合 db.log.drop(); // 2. 建立固定集合,size < 78 , 验证是否建立一个256的大小 db.createCollection("log", { capped: true, size: 78 }); // 2. 插入200条数据 for (var i = 0; i < 200; i++) { db.log.insertOne({ "_id": i + 1, "userId": Math.floor(Math.random() * 1000), "content": "登陆" + ("0000" + i).slice(-4), "createTime": new Date(), }); }
查看集合统计
db.log.stats()
能够看出log集合使用了234个字节(78 * 3),也即3个文档的大小,最大能使用大小是256
3. 查询固定集合
Mongodb若没指定排序字段,是按存入顺序检索,能够使用.sort( { $natural: -1 } )改变输出顺序
db.log.find({}).sort( { $natural: -1 } )
4. 将非固定集合转换固定集合
将order转换试试
db.runCommand({"convertToCapped": "order", size: 8096});
查看order集合统计
只剩下90条数据
转发请标明出处:https://www.cnblogs.com/WilsonPan/p/12692642.html