1. collection 名称mycol, 数据初始化
//1
{
"_id": ObjectId("5e05fe4a32780f42806a80c5"),
"author": "tom",
"books": [
{ "type": "IT类", "name": "mongodb", "price": NumberInt("100") },
{ "type": "IT类", "name": "java", "price": NumberInt("50") },
{ "type": "文学类", "name": "红楼梦", "price": NumberInt("20") }
],
"year": NumberInt("2018")
}
//2
{
"_id": ObjectId("5e05fe6032780f42806a80c7"),
"author": "tom",
"books": [ { "type": "IT类", "name": "程序员的修养", "price": NumberInt("30") },
{ "type": "文学类", "name": "简爱", "price": NumberInt("50") },
{ "type": "哲学类", "name": "西方哲学史", "price": NumberInt("20") }
],
"year": "2019"
}
//3
{
"_id": ObjectId("5e05fe6332780f42806a80c9"),
"author": "jack",
"books": [ { "type": "IT类", "name": "程序员的修养", "price": NumberInt("30") },
{ "type": "文学类", "name": "简爱", "price": NumberInt("50") },
{ "type": "哲学类", "name": "西方哲学史", "price": NumberInt("20") }
],
"year": "2019"
}
// 4
{
"_id": ObjectId("5e0854c432780f0d80c7bb43"),
"author": "jack",
"books": [ { "type": "IT类", "name": "程序员的修养", "price": NumberInt("30") },
{ "type": "IT类", "name": "高并发编程", "price": NumberInt("50") }
],
"year": "2018"
}
2. 查询2018年每个人在每种书的类别下写了多少本书?
2.1 查询2018年, $match相当sql的where
db.mycol.aggregate([
{ $match: {year:2018}}
]);
2.2 书的结构是数组,没法group, 要平铺展开。数组有多个元素,就会拆成多行
db.mycol.aggregate([
{ $match: {year:2018}},
{$unwind: "$books" },
]);
2.3 按书的作者和书的分类group by
db.mycol.aggregate([
{ $match: {year:2018}},
{$unwind: "$books" },
{$group:{"_id":{"author": "$author", "bookType":"$books.type"}, count:{$sum:1}}}
]);
2.4 控制查询结果的显示那些列, 相当于定义sql的查询结果(控制显示那些列,列的别名)
db.mycol.aggregate([
{ $match: {year:2018}},
{$unwind: "$books" },
{$group:{"_id":{"author": "$author", "bookType":"$books.type"},count:{$sum:1} }},
{$project:{ "_id":0, "author":"$_id.author" , "bookType":"$_id.bookType", "bookTypeCnt":"$count"}}
]);
2.5 按书的作者排序
db.mycol.aggregate([
{ $match: {year:2018}},
{$unwind: "$books" },
{$group:{"_id":{"author": "$author", "bookType":"$books.type"},count:{$sum:1} }},
{$project:{ "_id":0, "author":"$_id.author" , "bookType":"$_id.bookType", "bookTypeCnt":"$count"}},
{$sort:{author:-1}}
]);
最终的查询结果
// 1
{
"author": "tom",
"bookType": "IT类",
"bookTypeCnt": 3
}
// 2
{
"author": "tom",
"bookType": "文学类",
"bookTypeCnt": 2
}
// 3
{
"author": "tom",
"bookType": "哲学类",
"bookTypeCnt": 1
}
// 4
{
"author": "jack",
"bookType": "文学类",
"bookTypeCnt": 1
}
// 5
{
"author": "jack",
"bookType": "哲学类",
"bookTypeCnt": 1
}
// 6
{
"author": "jack",
"bookType": "IT类",
"bookTypeCnt": 3
}
3. 查询每个人写了多少不类别的书籍。
前三步和第二个查询是一样的。 也是先$ m a t c h match match, 再$unwind平铺展开, 然后按author、type 进行group by。
db.mycol.aggregate([
{ $match: {year:2018}},
{$unwind: "$books" },
{$group:{"_id":{"author": "$author", "bookType":"$books.type"} }}
]);
结果
// 1
{
"_id": {
"author": "jack",
"bookType": "文学类"
}
}
// 2
{
"_id": {
"author": "tom",
"bookType": "IT类"
}
}
// 3
{
"_id": {
"author": "tom",
"bookType": "文学类"
}
}
// 4
{
"_id": {
"author": "tom",
"bookType": "哲学类"
}
}
// 5
{
"_id": {
"author": "jack",
"bookType": "哲学类"
}
}
// 6
{
"_id": {
"author": "jack",
"bookType": "IT类"
}
}
然后再按author group by分组count, 去掉相同类别的
db.mycol.aggregate([
{ $match: {year:2018}},
{$unwind: "$books" },
{$group:{"_id":{"author": "$author", "bookType":"$books.type"} }},
{$group:{"_id":{"author": "$_id.author" }, "typeCnt":{$sum:1}}}
]);
查询结果:
// 1
{
"_id": {
"author": "tom"
},
"typeCnt": 3
}
// 2
{
"_id": {
"author": "jack"
},
"typeCnt": 3
}