第四章 Mongodb聚合函数


插入 测试数据

for(var j=;j<;j++){
for(var i=1;i<3;i++){
var person={
Name:"jack"+i,
Age:i,
Address:["henan","wuhan"],
Course:[
{Name:"shuxue",Score:i},
{Name:"wuli",Score:i}
]
}
db.DemoTest.Person.insert(person)
}
}

Count

db.DemoTest.Person.count({Name:"jack1"})

返回数量

distinct

db.DemoTest.Person.distinct("Name")

返回不重复的Name值。

group

例子:按照Name分组,条件是Age大于46

db.DemoTest.Person.group({
"key":{"Name":true}, -----分组的keky
"initial":{"Person":[]},-------每组分享的一个”初始化函数“
"$reduce":function(cur,prev){ ------这个函数的第一个参数是当前的文档对象,第二个参数是上一次function操作的累计对象,第一次为initial中的{”person“:[]}。有多少个文档, $reduce就会调用多少次 prev.Person.push(cur);
},
"finalize":function(prev){ ---返回每组的数量
prev.count=prev.Person.length;
},
"condition":{"Age":{"$lt":46}} -----过滤条件
})

返回结果如下:

/*  */
{
"0" : {
"Name" : "jack1",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9d6"),
"Name" : "jack1",
"Age" : 1,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 1
},
{
"Name" : "wuli",
"Score" : 1
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba07"),
"Name" : "jack1",
"Age" : 1,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 1
},
{
"Name" : "wuli",
"Score" : 1
}
]
}
],
"count" :
},
"" : {
"Name" : "jack2",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9d7"),
"Name" : "jack2",
"Age" : 2,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 2
},
{
"Name" : "wuli",
"Score" : 2
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba08"),
"Name" : "jack2",
"Age" : 2,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 2
},
{
"Name" : "wuli",
"Score" : 2
}
]
}
],
"count" :
},
"" : {
"Name" : "jack3",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9d8"),
"Name" : "jack3",
"Age" : 3,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 3
},
{
"Name" : "wuli",
"Score" : 3
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba09"),
"Name" : "jack3",
"Age" : 3,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 3
},
{
"Name" : "wuli",
"Score" : 3
}
]
}
],
"count" :
},
"" : {
"Name" : "jack4",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9d9"),
"Name" : "jack4",
"Age" : 4,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 4
},
{
"Name" : "wuli",
"Score" : 4
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba0a"),
"Name" : "jack4",
"Age" : 4,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 4
},
{
"Name" : "wuli",
"Score" : 4
}
]
}
],
"count" :
},
"" : {
"Name" : "jack5",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9da"),
"Name" : "jack5",
"Age" : 5,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 5
},
{
"Name" : "wuli",
"Score" : 5
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba0b"),
"Name" : "jack5",
"Age" : 5,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 5
},
{
"Name" : "wuli",
"Score" : 5
}
]
}
],
"count" :
},
"" : {
"Name" : "jack6",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9db"),
"Name" : "jack6",
"Age" : 6,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 6
},
{
"Name" : "wuli",
"Score" : 6
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba0c"),
"Name" : "jack6",
"Age" : 6,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 6
},
{
"Name" : "wuli",
"Score" : 6
}
]
}
],
"count" :
},
"" : {
"Name" : "jack7",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9dc"),
"Name" : "jack7",
"Age" : 7,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 7
},
{
"Name" : "wuli",
"Score" : 7
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba0d"),
"Name" : "jack7",
"Age" : 7,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 7
},
{
"Name" : "wuli",
"Score" : 7
}
]
}
],
"count" :
},
"" : {
"Name" : "jack8",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9dd"),
"Name" : "jack8",
"Age" : 8,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 8
},
{
"Name" : "wuli",
"Score" : 8
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba0e"),
"Name" : "jack8",
"Age" : 8,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 8
},
{
"Name" : "wuli",
"Score" : 8
}
]
}
],
"count" :
},
"" : {
"Name" : "jack9",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9de"),
"Name" : "jack9",
"Age" : 9,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 9
},
{
"Name" : "wuli",
"Score" : 9
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba0f"),
"Name" : "jack9",
"Age" : 9,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 9
},
{
"Name" : "wuli",
"Score" : 9
}
]
}
],
"count" :
},
"" : {
"Name" : "jack10",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9df"),
"Name" : "jack10",
"Age" : 10,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 10
},
{
"Name" : "wuli",
"Score" : 10
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba10"),
"Name" : "jack10",
"Age" : 10,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 10
},
{
"Name" : "wuli",
"Score" : 10
}
]
}
],
"count" :
},
"" : {
"Name" : "jack11",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e0"),
"Name" : "jack11",
"Age" : 11,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 11
},
{
"Name" : "wuli",
"Score" : 11
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba11"),
"Name" : "jack11",
"Age" : 11,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 11
},
{
"Name" : "wuli",
"Score" : 11
}
]
}
],
"count" :
},
"" : {
"Name" : "jack12",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e1"),
"Name" : "jack12",
"Age" : 12,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 12
},
{
"Name" : "wuli",
"Score" : 12
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba12"),
"Name" : "jack12",
"Age" : 12,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 12
},
{
"Name" : "wuli",
"Score" : 12
}
]
}
],
"count" :
},
"" : {
"Name" : "jack13",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e2"),
"Name" : "jack13",
"Age" : 13,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 13
},
{
"Name" : "wuli",
"Score" : 13
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba13"),
"Name" : "jack13",
"Age" : 13,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 13
},
{
"Name" : "wuli",
"Score" : 13
}
]
}
],
"count" :
},
"" : {
"Name" : "jack14",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e3"),
"Name" : "jack14",
"Age" : 14,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 14
},
{
"Name" : "wuli",
"Score" : 14
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba14"),
"Name" : "jack14",
"Age" : 14,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 14
},
{
"Name" : "wuli",
"Score" : 14
}
]
}
],
"count" :
},
"" : {
"Name" : "jack15",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e4"),
"Name" : "jack15",
"Age" : 15,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 15
},
{
"Name" : "wuli",
"Score" : 15
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba15"),
"Name" : "jack15",
"Age" : 15,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 15
},
{
"Name" : "wuli",
"Score" : 15
}
]
}
],
"count" :
},
"" : {
"Name" : "jack16",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e5"),
"Name" : "jack16",
"Age" : 16,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 16
},
{
"Name" : "wuli",
"Score" : 16
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba16"),
"Name" : "jack16",
"Age" : 16,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 16
},
{
"Name" : "wuli",
"Score" : 16
}
]
}
],
"count" :
},
"" : {
"Name" : "jack17",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e6"),
"Name" : "jack17",
"Age" : 17,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 17
},
{
"Name" : "wuli",
"Score" : 17
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba17"),
"Name" : "jack17",
"Age" : 17,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 17
},
{
"Name" : "wuli",
"Score" : 17
}
]
}
],
"count" :
},
"" : {
"Name" : "jack18",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e7"),
"Name" : "jack18",
"Age" : 18,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 18
},
{
"Name" : "wuli",
"Score" : 18
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba18"),
"Name" : "jack18",
"Age" : 18,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 18
},
{
"Name" : "wuli",
"Score" : 18
}
]
}
],
"count" :
},
"" : {
"Name" : "jack19",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e8"),
"Name" : "jack19",
"Age" : 19,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 19
},
{
"Name" : "wuli",
"Score" : 19
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba19"),
"Name" : "jack19",
"Age" : 19,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 19
},
{
"Name" : "wuli",
"Score" : 19
}
]
}
],
"count" :
},
"" : {
"Name" : "jack20",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9e9"),
"Name" : "jack20",
"Age" : 20,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 20
},
{
"Name" : "wuli",
"Score" : 20
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba1a"),
"Name" : "jack20",
"Age" : 20,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 20
},
{
"Name" : "wuli",
"Score" : 20
}
]
}
],
"count" :
},
"" : {
"Name" : "jack21",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9ea"),
"Name" : "jack21",
"Age" : 21,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 21
},
{
"Name" : "wuli",
"Score" : 21
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba1b"),
"Name" : "jack21",
"Age" : 21,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 21
},
{
"Name" : "wuli",
"Score" : 21
}
]
}
],
"count" :
},
"" : {
"Name" : "jack22",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9eb"),
"Name" : "jack22",
"Age" : 22,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 22
},
{
"Name" : "wuli",
"Score" : 22
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba1c"),
"Name" : "jack22",
"Age" : 22,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 22
},
{
"Name" : "wuli",
"Score" : 22
}
]
}
],
"count" :
},
"" : {
"Name" : "jack23",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9ec"),
"Name" : "jack23",
"Age" : 23,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 23
},
{
"Name" : "wuli",
"Score" : 23
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba1d"),
"Name" : "jack23",
"Age" : 23,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 23
},
{
"Name" : "wuli",
"Score" : 23
}
]
}
],
"count" :
},
"" : {
"Name" : "jack24",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9ed"),
"Name" : "jack24",
"Age" : 24,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 24
},
{
"Name" : "wuli",
"Score" : 24
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba1e"),
"Name" : "jack24",
"Age" : 24,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 24
},
{
"Name" : "wuli",
"Score" : 24
}
]
}
],
"count" :
},
"" : {
"Name" : "jack25",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9ee"),
"Name" : "jack25",
"Age" : 25,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 25
},
{
"Name" : "wuli",
"Score" : 25
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba1f"),
"Name" : "jack25",
"Age" : 25,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 25
},
{
"Name" : "wuli",
"Score" : 25
}
]
}
],
"count" :
},
"" : {
"Name" : "jack26",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9ef"),
"Name" : "jack26",
"Age" : 26,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 26
},
{
"Name" : "wuli",
"Score" : 26
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba20"),
"Name" : "jack26",
"Age" : 26,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 26
},
{
"Name" : "wuli",
"Score" : 26
}
]
}
],
"count" :
},
"" : {
"Name" : "jack27",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f0"),
"Name" : "jack27",
"Age" : 27,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 27
},
{
"Name" : "wuli",
"Score" : 27
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba21"),
"Name" : "jack27",
"Age" : 27,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 27
},
{
"Name" : "wuli",
"Score" : 27
}
]
}
],
"count" :
},
"" : {
"Name" : "jack28",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f1"),
"Name" : "jack28",
"Age" : 28,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 28
},
{
"Name" : "wuli",
"Score" : 28
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba22"),
"Name" : "jack28",
"Age" : 28,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 28
},
{
"Name" : "wuli",
"Score" : 28
}
]
}
],
"count" :
},
"" : {
"Name" : "jack29",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f2"),
"Name" : "jack29",
"Age" : 29,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 29
},
{
"Name" : "wuli",
"Score" : 29
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba23"),
"Name" : "jack29",
"Age" : 29,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 29
},
{
"Name" : "wuli",
"Score" : 29
}
]
}
],
"count" :
},
"" : {
"Name" : "jack30",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f3"),
"Name" : "jack30",
"Age" : 30,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 30
},
{
"Name" : "wuli",
"Score" : 30
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba24"),
"Name" : "jack30",
"Age" : 30,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 30
},
{
"Name" : "wuli",
"Score" : 30
}
]
}
],
"count" :
},
"" : {
"Name" : "jack31",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f4"),
"Name" : "jack31",
"Age" : 31,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 31
},
{
"Name" : "wuli",
"Score" : 31
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba25"),
"Name" : "jack31",
"Age" : 31,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 31
},
{
"Name" : "wuli",
"Score" : 31
}
]
}
],
"count" :
},
"" : {
"Name" : "jack32",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f5"),
"Name" : "jack32",
"Age" : 32,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 32
},
{
"Name" : "wuli",
"Score" : 32
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba26"),
"Name" : "jack32",
"Age" : 32,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 32
},
{
"Name" : "wuli",
"Score" : 32
}
]
}
],
"count" :
},
"" : {
"Name" : "jack33",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f6"),
"Name" : "jack33",
"Age" : 33,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 33
},
{
"Name" : "wuli",
"Score" : 33
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba27"),
"Name" : "jack33",
"Age" : 33,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 33
},
{
"Name" : "wuli",
"Score" : 33
}
]
}
],
"count" :
},
"" : {
"Name" : "jack34",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f7"),
"Name" : "jack34",
"Age" : 34,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 34
},
{
"Name" : "wuli",
"Score" : 34
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba28"),
"Name" : "jack34",
"Age" : 34,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 34
},
{
"Name" : "wuli",
"Score" : 34
}
]
}
],
"count" :
},
"" : {
"Name" : "jack35",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f8"),
"Name" : "jack35",
"Age" : 35,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 35
},
{
"Name" : "wuli",
"Score" : 35
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba29"),
"Name" : "jack35",
"Age" : 35,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 35
},
{
"Name" : "wuli",
"Score" : 35
}
]
}
],
"count" :
},
"" : {
"Name" : "jack36",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9f9"),
"Name" : "jack36",
"Age" : 36,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 36
},
{
"Name" : "wuli",
"Score" : 36
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba2a"),
"Name" : "jack36",
"Age" : 36,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 36
},
{
"Name" : "wuli",
"Score" : 36
}
]
}
],
"count" :
},
"" : {
"Name" : "jack37",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9fa"),
"Name" : "jack37",
"Age" : 37,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 37
},
{
"Name" : "wuli",
"Score" : 37
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba2b"),
"Name" : "jack37",
"Age" : 37,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 37
},
{
"Name" : "wuli",
"Score" : 37
}
]
}
],
"count" :
},
"" : {
"Name" : "jack38",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9fb"),
"Name" : "jack38",
"Age" : 38,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 38
},
{
"Name" : "wuli",
"Score" : 38
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba2c"),
"Name" : "jack38",
"Age" : 38,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 38
},
{
"Name" : "wuli",
"Score" : 38
}
]
}
],
"count" :
},
"" : {
"Name" : "jack39",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9fc"),
"Name" : "jack39",
"Age" : 39,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 39
},
{
"Name" : "wuli",
"Score" : 39
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba2d"),
"Name" : "jack39",
"Age" : 39,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 39
},
{
"Name" : "wuli",
"Score" : 39
}
]
}
],
"count" :
},
"" : {
"Name" : "jack40",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9fd"),
"Name" : "jack40",
"Age" : 40,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 40
},
{
"Name" : "wuli",
"Score" : 40
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba2e"),
"Name" : "jack40",
"Age" : 40,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 40
},
{
"Name" : "wuli",
"Score" : 40
}
]
}
],
"count" :
},
"" : {
"Name" : "jack41",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9fe"),
"Name" : "jack41",
"Age" : 41,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 41
},
{
"Name" : "wuli",
"Score" : 41
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba2f"),
"Name" : "jack41",
"Age" : 41,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 41
},
{
"Name" : "wuli",
"Score" : 41
}
]
}
],
"count" :
},
"" : {
"Name" : "jack42",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2b9ff"),
"Name" : "jack42",
"Age" : 42,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 42
},
{
"Name" : "wuli",
"Score" : 42
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba30"),
"Name" : "jack42",
"Age" : 42,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 42
},
{
"Name" : "wuli",
"Score" : 42
}
]
}
],
"count" :
},
"" : {
"Name" : "jack43",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2ba00"),
"Name" : "jack43",
"Age" : 43,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 43
},
{
"Name" : "wuli",
"Score" : 43
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba31"),
"Name" : "jack43",
"Age" : 43,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 43
},
{
"Name" : "wuli",
"Score" : 43
}
]
}
],
"count" :
},
"" : {
"Name" : "jack44",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2ba01"),
"Name" : "jack44",
"Age" : 44,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 44
},
{
"Name" : "wuli",
"Score" : 44
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba32"),
"Name" : "jack44",
"Age" : 44,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 44
},
{
"Name" : "wuli",
"Score" : 44
}
]
}
],
"count" :
},
"" : {
"Name" : "jack45",
"Person" : [
{
"_id" : ObjectId("54461dce69f872cf5ea2ba02"),
"Name" : "jack45",
"Age" : 45,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 45
},
{
"Name" : "wuli",
"Score" : 45
}
]
},
{
"_id" : ObjectId("54461dce69f872cf5ea2ba33"),
"Name" : "jack45",
"Age" : 45,
"Address" : [
"henan",
"wuhan"
],
"Course" : [
{
"Name" : "shuxue",
"Score" : 45
},
{
"Name" : "wuli",
"Score" : 45
}
]
}
],
"count" :
}
}

返回的json

mapReduce

mapReduce其实是一种编程模型,用在分布式计算中,其中有一个“map”函数,一个”reduce“函数。

map:

这个称为映射函数,里面会调用emit(key,value),集合会按照你指定的key进行映射分组。

reduce:

这个称为简化函数,会对map分组后的数据进行分组简化,注意:在reduce(key,value)中的key就是

emit中的key,vlaue为emit分组后的emit(value)的集合,这里也就是很多{"count":1}的数组。

mapReduce:

这个就是最后执行的函数了,参数为map,reduce和一些可选参数。

在MongoDB存储的文档上执行聚合操作非常有用,这种方式的一个限制是聚合函数(比如,SUM、AVG、MIN、MAX)需要通过mapper和reducer函数来定制化实现。

MongoDB没有原生态的用户自定义函数(UDFs)支持。但是它允许使用db.system.js.save命令来创建并保存JavaScript函数,JavaScript函数可以在MapReduce中复用。

第一种统计方式--对应集合直接统计

1.在MongoDB javascript Shell中对Array对象进行了一些扩展,其中新增sum方法,以方便统计数据之用的。

Array.sum=function(arr){
if(arr.length == 0)
return null;
var s = arr[0];
for(var i = 1; i < arr.length; i++)
s += arr[i];
return s;
}

2.例子:按照名称分组,统计每组年龄的和,条件是年龄小于2.

如果统计数量:var map = function(){ emit(this.Name, 1); }   其实是让值永远为1

var map = function(){ emit(this.Name, this.Age); }
var reduce = function( key, values ){ return Array.sum(values); }
var options = {query: { Age: {$lt: 2} }, out: { inline : 1 }}
db.Person.mapReduce(map,reduce,options)

结果如下

/*  */
{
"results" : [
{
"_id" : "jack1",
"value" : 2
}
],
"timeMillis" : ,
"counts" : {
"input" : 2,
"emit" : 2,
"reduce" : 1,
"output" : 1
},
"ok" : ,
"_o" : {
"results" : [
{
"_id" : "jack1",
"value" : 2
}
],
"timeMillis" : ,
"counts" : {
"input" : 2,
"emit" : 2,
"reduce" : 1,
"output" : 1
},
"ok" :
},
"_keys" : [
"results",
"timeMillis",
"counts",
"ok"
],
"_db" : {
"_mongo" : "connection to localhost:27017{ SSL: { sslSupport: false, sslPEMKeyFile: \"\" } }{ SSH: { host: \"\", port: 22, user: \"\", password: \"\", publicKey: { publicKey: \"\", privateKey: \"\", passphrase: \"\" }, currentMethod: } }",
"_name" : "local",
"system.indexes" : "local.system.indexes",
"prototype" : "local.prototype",
"startup_log" : "local.startup_log",
"TestCollection" : "local.TestCollection",
"TestCollection.Person" : "local.TestCollection.Person",
"DemoTest" : "local.DemoTest",
"DemoTest.Person" : "local.DemoTest.Person",
"system" : "local.system",
"system.js" : "local.system.js",
"mythings" : "local.mythings",
"age_totals" : "local.age_totals",
"Person" : "local.Person"
}
}

结果json

分析一下:
1. map部分
作用:用于分组的。
emit(param1, param2)
param1:需要分组的字段,this.字段名。
param2:需要进行统计的字段,this.字段名。

2. reduce部分
作用:处理需要统计的字段
var reduce = function(key, values){
......统计字段处理
}
key: 指分组字段(emit的param1)对应的值
values:指需要统计的字段(emit的param2)值组成的数组

简单介绍统计常用的方法:
* 对数值类型进行求和

var reduce = function(key, values){
return Array.sum(values);
}

* 对字符串类型进行拼凑

var reduce = function(key, values){
return values.join(', ');
}

3. options部分
{ query: { age: {$lt: 25} }, out: "name_totals" }
query:先筛选符合条件的记录出来,再进行分组统计。
out:将分组统计后的结果输出到哪个集合当中。
默认情况下,out所指定的集合在数据库断开连接后再次打开时,依旧存在,并保留之前的所有记录的。

4. 执行分组统计
>db.集合名.mapReduce( map, reduce, options )

第二种统计方式--命令统计

1.命令如下:

注意:out参数 out:"Person_Name" 代表会创建一个临时表Person_Name 然后再从临时表中查找,out:{inline:1} 代表直接显示在当前命令执行的结果中

var map = function(){ emit(this.Name, this.Age); }
var reduce = function( key, values ){ return Array.sum(values); }
db.runCommand({
mapreduce:"Person",
map:map,
reduce:reduce,
out:"Person_Name",
keeptemp: false,
query: { Age:{ $lt: 2 }},
sort:{ Name:1},
limit:3
})

解析:
mapreduce:
分组统计的集合名
eg:
mapreduce: 'mythings'
不能写成mapreduce: mythings,否则报异常:mythings is not defined

map,reduce :
同上,不做阐述

out :
将分组统计结果输出到某个集合。
注意:不能缺省,必须指定名称,否则报错,报错如下:
“exception: 'out' has to be a string or an object”

keeptemp :
是否保留临时集合(指out指定的集合)
keeptemp:false时会在数据库断开连接后,MongoDB会移除该集合的所有记录。而不是删除。
keeptemp:true时即使数据库断开连接后,再次连接上,该临时集合依旧保持之前所有记录。
keeptemp默认值为true。

query :
筛选记录后,再进行分组统计
eg:
query: { age:{ $lt: 25 }}

sort :
对分组统计的集合进行排序,也即先排序,后再执行分组统计的。
注意:这里的排序需要用到索引,必须先创建索引。

limit :

对分组统计的集合先进行限制返回记录的条数,然后再去进行统计操作。注意:不要理解成对统计后的结果进行限制返回记录条数。

verbose :
显示时间统计信息,取值为true/false

参考资料

http://www.cnblogs.com/shanyou/archive/2012/08/05/2624073.html

http://www.52ij.com/jishu/3925.html

Mongodb学习笔记四(Mongodb聚合函数)的更多相关文章

  1. Mongodb学习笔记一(Mongodb环境配置)

    Mongodb学习 说明: MongoDB由databases组成,database由collections组成,collection由documents组成,document由fileds组成.Mo ...

  2. MongoDB学习总结(三) —— 常用聚合函数

    上一篇介绍了MongoDB增删改查命令的基本用法,这一篇来学习一下MongoDB的一些基本聚合函数. 下面我们直奔主题,用简单的实例依次介绍一下. > count() 函数 集合的count函数 ...

  3. MongoDB学习笔记一(MongoDB介绍 + 基本指令 + 查询语句)

    什么是MongoDB MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供可扩 ...

  4. MongoDb 学习笔记(一) --- MongoDb 数据库介绍、安装、使用

    1.数据库和文件的主要区别 . 数据库有数据库表.行和列的概念,让我们存储操作数据更方便 . 数据库提供了非常方便的接口,可以让 nodejs.php java .net 很方便的实现增加修改删除功能 ...

  5. MongoDB学习笔记(一) MongoDB介绍及安装(摘)

    MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发.Mongo的官方网 ...

  6. Mongodb学习笔记二(Mongodb基本命令)

    第二章 基本命令 一.Mongodb命令 说明:Mongodb命令是区分大小写的,使用的命名规则是驼峰命名法. 对于database和collection无需主动创建,在插入数据时,如果databas ...

  7. MongoDB学习笔记五:聚合

    『count』count是最简单的聚合工具,返回集合中的文档数量:> db.foo.count()0> db.foo.insert({"x" : 1})> db. ...

  8. MongoDB学习笔记四:索引

    索引就是用来加速查询的.创建数据库索引就像确定如何组织书的索引一样.但是你的优势是知道今后做何种查询,以及哪些内容需要快速查找.比如:所有的查询都包括"date"键,那么很可能(至 ...

  9. MongoDB学习笔记(四) 用MongoDB的文档结构描述数据关系

    MongoDB的集合(collection)可以看做关系型数据库的表,文档对象(document)可以看做关系型数据库的一条记录.但两者并不完全对等.表的结构是固定的,MongoDB集合并没有这个约束 ...

随机推荐

  1. jQuery静态方法noop,camelCase,nodeName,trim使用和源码分析

    noop方法 jQuery.noop()函数是一个空函数,它什么也不做. 当某些时候你需要传入函数参数,而且希望它什么也不做的时候,你可以使用该函数,也无需再新建一个空的函数. 示例: // 传入一个 ...

  2. web前端交互性易用性说明

    总结一下我们在web前端开发过程中总是强调交互性.易用性的情况分析说明.个人觉得web前端的易用交互也就是我们所说人性化操作.不外乎希望达到的效果为:界面风格简洁明了.重点突出:操作简单,直观可见.当 ...

  3. iOS UIScrollView的使用

    一.为什么要用UIScrollView? 移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的内容也相当有限当展示的内容较多,超出一个屏幕时,用户可通过滚动手势来查看屏幕以外的内容普通的UIVie ...

  4. C# Stream 和 byte[] 之间的转换

    一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream( ...

  5. SharePoint 2013 搜索高级配置(Search Scope)

    前言:SharePoint 2013集成了Fast Search,而后在配置上与2010及之前版本都有一些区别,如果需要开启搜索的文档,请参考我之前写的博客,博客地址附后.下面,我们开始了解下,Sha ...

  6. MRC下多个对象的内存管理

    //set方法传递进来对象的生命周期,要求是在当前对象销毁之前,它一直存在就好- (void)setCar:(Car *)car{ //1.判断set方法传递进来的值是否与成员变量中保存的是同一个对象 ...

  7. VS单元测试

    弄了好久才明白 ,但是收获确实挺大的,话不多说,直接上图. 1 打开VS建立项目 2 建立一个类 3 点击创建单元测试 4 在运行模块里重新输入代码 5 在空白处 点击鼠标右键 选择运行测试 6 测试 ...

  8. Zookeeper(一)从抽屉算法到Quorum (NRW)算法

    一.抽屉算法 抽屉算法,又名鸽巢原理,它是德国数学家狄利克雷首先明确的提出来并用以证明一些数论中的问题,因此,也称为狄利克雷原则.它是组合数学中一个重要的原理. 具体算法讲的是: 第一抽屉算法: 如果 ...

  9. 【转】jquery 中scrollTop在Firefox下不起作用

    原文链接:http://stackoverflow.com/questions/8149155/animate-scrolltop-not-working-in-firefox Animate scr ...

  10. percona教程:MySQL GROUP_CONCAT的使用

    percona有一篇blog: The power of MySQL GROUP_CONCAT 比较详细地介绍了GROUP_CONCAT函数的用法.简单地翻译了一下. 假设你有4名工程师,这周他们为6 ...