第四章 Mongodb聚合函数
插入 测试数据
for(var j=1;j<3;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 */ { "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" : 2 }, "1" : { "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" : 2 }, "2" : { "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" : 2 }, "3" : { "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" : 2 }, "4" : { "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" : 2 }, "5" : { "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" : 2 }, "6" : { "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" : 2 }, "7" : { "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" : 2 }, "8" : { "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" : 2 }, "9" : { "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" : 2 }, "10" : { "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" : 2 }, "11" : { "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" : 2 }, "12" : { "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" : 2 }, "13" : { "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" : 2 }, "14" : { "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" : 2 }, "15" : { "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" : 2 }, "16" : { "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" : 2 }, "17" : { "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" : 2 }, "18" : { "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" : 2 }, "19" : { "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" : 2 }, "20" : { "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" : 2 }, "21" : { "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" : 2 }, "22" : { "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" : 2 }, "23" : { "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" : 2 }, "24" : { "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" : 2 }, "25" : { "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" : 2 }, "26" : { "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" : 2 }, "27" : { "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" : 2 }, "28" : { "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" : 2 }, "29" : { "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" : 2 }, "30" : { "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" : 2 }, "31" : { "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" : 2 }, "32" : { "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" : 2 }, "33" : { "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" : 2 }, "34" : { "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" : 2 }, "35" : { "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" : 2 }, "36" : { "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" : 2 }, "37" : { "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" : 2 }, "38" : { "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" : 2 }, "39" : { "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" : 2 }, "40" : { "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" : 2 }, "41" : { "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" : 2 }, "42" : { "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" : 2 }, "43" : { "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" : 2 }, "44" : { "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" : 2 } }
返回的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)
结果如下
/* 0 */ { "results" : [ { "_id" : "jack1", "value" : 2 } ], "timeMillis" : 0, "counts" : { "input" : 2, "emit" : 2, "reduce" : 1, "output" : 1 }, "ok" : 1, "_o" : { "results" : [ { "_id" : "jack1", "value" : 2 } ], "timeMillis" : 0, "counts" : { "input" : 2, "emit" : 2, "reduce" : 1, "output" : 1 }, "ok" : 1 }, "_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: 0 } }", "_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