db.collection.estimatedDocumentCou
nt()
|
返回集合或视图中所有文档的计数
|
db.collection.count()
|
返回与find()集合或视图的查询匹配的文档计数 。等同于
db.collection.find(query).count()构造
|
db.collection.distinct()
|
在单个集合或视图中查找指定字段的不同值,并在数组中
返回结果。
|
#检索books集合中所有文档的计数
db.books.estimatedDocumentCount()
#计算与查询匹配的所有文档
db.books.count({favCount:{$gt:50}})
#返回不同type的数组
db.books.distinct("type")
#返回收藏数大于90的文档不同type的数组
db.books.distinct("type",{favCount:{$gt:90}})
pipeline = [$stage1, $stage2, ...$stageN];
db.collection.aggregate(pipeline, {options})
聚合管道包含非常丰富的聚合阶段,下面是最常用的聚合阶段
var tags = ["nosql","mongodb","document","developer","popular"];
var types = ["technology","sociality","travel","novel","literature"];
var books=[];
for(var i=0;i<50;i++){
var typeIdx = Math.floor(Math.random()*types.length);
var tagIdx = Math.floor(Math.random()*tags.length);
var tagIdx2 = Math.floor(Math.random()*tags.length);
var favCount = Math.floor(Math.random()*100);
var username = "xx00"+Math.floor(Math.random()*10);
var age = 20 + Math.floor(Math.random()*15);
var book = {
title: "book‐"+i,
type: types[typeIdx],
tag: [tags[tagIdx],tags[tagIdx2]],
favCount: favCount,
author: {name:username,age:age}
};
books.push(book)
}
db.books.insertMany(books);
db.books.aggregate([{$project:{name:"$title"}}])
db.books.aggregate([{$project:{name:"$title",_id:0,type:1,author:1}}])
db.books.aggregate([
{$project:{name:"$title",_id:0,type:1,"author.name":1}}
])
或者
db.books.aggregate([
{$project:{name:"$title",_id:0,type:1,author:{name:1}}}
])
db.books.aggregate([{$match:{type:"technology"}}])
db.books.aggregate([
{$match:{type:"technology"}},
{$project:{name:"$title",_id:0,type:1,author:{name:1}}}
])
计数并返回与查询匹配的结果数
db.books.aggregate([
{$match:{type:"technology"}},
{$count: "type_count"}
])
{ $group: { _id: , : { :
}, ... } }
名称
|
描述 | 类比sql |
$avg
|
计算均值
|
avg
|
$first
|
返回每组第一个文档,如果有排序,按照排序,如果没有按照默认的 存储的顺序的第一个文档。
|
limit0,1
|
$last
|
返回每组最后一个文档,如果有排序,按照排序,如果没有按照默认 的存储的顺序的最后个文档
|
|
$max
|
根据分组,获取集合中所有文档对应值得最大值
|
max
|
$min
|
根据分组,获取集合中所有文档对应值得最小值。
|
min
|
$pus
|
将指定的表达式的值添加到一个数组中
|
|
$addToSet
|
将表达式的值添加到一个集合中(无重复值,无序)
|
|
$sum
|
计算总和
|
sum
|
$stdDevPop
|
返回输入值的总体标准偏差(population standard deviation)
|
|
$stdDevSamp
|
返回输入值的样本标准偏差(the sample standard deviation)
|
db.books.aggregate([
{$group:{_id:null,count:{$sum:1},pop:{$sum:"$favCount"},avg:{$avg:"$favCount"}}}
])
db.books.aggregate([
{$group:{_id:"$author.name",pop:{$sum:"$favCount"}}}
])
db.books.aggregate([
{$group:{_id:{name:"$author.name",title:"$title"},pop:{$sum:"$favCount"}}}
])
db.books.aggregate([
{$group:{_id:"$author.name",types:{$addToSet:"$type"}}}
])
{
$unwind:
{
#要指定字段路径,在字段名称前加上$符并用引号括起来。
path: ,
#可选,一个新字段的名称用于存放元素的数组索引。该名称不能以$开头。
includeArrayIndex: ,
#可选,default :false,若为true,如果路径为空,缺少或为空数组,则$unwind输出文档
preserveNullAndEmptyArrays:
} }
db.books.aggregate([
{$match:{"author.name":"xx006"}},
{$unwind:"$tag"}
])
db.books.aggregate([
{$match:{"author.name":"xx006"}}
])
db.books.aggregate([
{$unwind:"$tag"},
{$group:{_id:"$author.name",types:{$addToSet:"$tag"}}}
])
db.books.aggregate([
{$limit : 5 }
])
db.books.aggregate([
{$skip : 5 }
])
{ $sort: { : , : ... } }
db.books.aggregate([
{$sort : {favCount:‐1,title:1}}
])
db.collection.aggregate([{
$lookup: {
from: "",
localField: "",
foreignField: "",
as: "
from
|
同一个数据库下等待被Join的集合。
|
localField
|
源集合中的match值,如果输入的集合中,某文档没有 localField 这个Key(Field),在处理的过程中,会默认为此文档含 有 localField:null的键值对。
|
foreignField
|
待Join的集合的match值,如果待Join的集合中,文档没有foreignField 值,在处理的过程中,会默认为此文档含有 foreignField:null的键值对。
|
as
|
为输出文档的新增值命名。如果输入的集合中已存在该值,则会覆盖掉
|
SELECT *,
db.customer.insert({customerCode:1,name:"customer1",phone:"13112345678",address:"test1"})
db.customer.insert({customerCode:2,name:"customer2",phone:"13112345679",address:"test2"})
db.order.insert({orderId:1,orderCode:"order001",customerCode:1,price:200})
db.order.insert({orderId:2,orderCode:"order002",customerCode:2,price:400})
db.orderItem.insert({itemId:1,productName:"apples",qutity:2,orderId:1})
db.orderItem.insert({itemId:2,productName:"oranges",qutity:2,orderId:1})
db.orderItem.insert({itemId:3,productName:"mangoes",qutity:2,orderId:1})
db.orderItem.insert({itemId:4,productName:"apples",qutity:2,orderId:2})
db.orderItem.insert({itemId:5,productName:"oranges",qutity:2,orderId:2})
db.orderItem.insert({itemId:6,productName:"mangoes",qutity:2,orderId:2})
db.customer.aggregate([
{$lookup: {
from: "order",
localField: "customerId",
foreignField: "customerId",
as: "customerOrder"
}
}
])
db.order.aggregate([
{$lookup: {
from: "customer",
localField: "customerCode",
foreignField: "customerCode",
as: "curstomer"
}
},
{$lookup: {
from: "orderItem",
localField: "orderId",
foreignField: "orderId",
as: "orderItem"
}
} ])
db.books.aggregate([
{$group:{_id:"$type",total:{$sum:1}}},
{$sort:{total:‐1}}
])
db.books.aggregate([
{$match:{favCount:{$gt:0}}},
{$unwind:"$tag"},
{$group:{_id:"$tag",total:{$sum:"$favCount"}}},
{$sort:{total:‐1}}
])
db.books.aggregate([{
$bucket:{
groupBy:"$favCount",
boundaries:[0,10,60,80,100],
default:"other",
output:{"count":{$sum:1}}
}
}])