MongoDB的高级操作:聚合和游标
(1)聚合操作:
<1>count
获取搜索结果的数据条数
<2>distinct
获取指定地段的集合,不允许重复
>
>
> db.student.find();
{ "_id" : ObjectId("55eb9d9d900da635cb33f220"), "name" : "liumengyang", "city" :
"dalian" }
{ "_id" : ObjectId("55eb9da9900da635cb33f221"), "name" : "lidongqing", "city" :
"dalian" }
{ "_id" : ObjectId("55ebd440900da635cb33f222"), "name" : "ketty", "city" : "heng
shui" }
>
>
>
> db.student.distinct("city");
[ "dalian", "hengshui" ]
<3>group
key:要进行分组的key
initial:每组都分享的一个初始化函数
$reduce:这个函数的第一个参数就是当前的文档对象,第二个参数是上一次function操作的累计对象,第一次为initial中的函数。
condition:过滤条件
finalize:每一组文档执行完毕以后,多会触发这个方法,可以用来在集合里面加上count属性【用来表名每一组的数据量】
使用实例:
> db.student.group({
... "key":{"city":true},
... "initial":{"person":[]},
... "$reduce":function(cur,prev){ prev.person.push(cur.name); },
... "condition":{"city":{$nin:["beijing"]}},
... "finalize":function(out){
...
... out.count = out.person.length;
... }
...
... })
[
{
"city" : "dalian",
"person" : [
"liumengyang",
"lidongqing"
],
"count" : 2
},
{
"city" : "hengshui",
"person" : [
"ketty"
],
"count" : 1
},
{
"city" : "handan",
"person" : [
"horse",
"xiaowanzi"
],
"count" : 2
},
{
"city" : "xingtai",
"person" : [
"bird"
],
"count" : 1
}
]
>
(4)mapreduce
mapreduce是一种编程模型,用在分布式计算中,包括两个函数:map函数和reduce函数
> var map = function(){
... emit(this.name,{count:1});
... }
>
>
> var reduce =
... function(key,value){
... var result = {count:0};
... for(var i=0;i<value.length;i++){
... result.count += value[i].count;
... }
... return result;
... }
>
>
> db.student.mapReduce(map,reduce,{"out":"collection"});
{
"result" : "collection",
"timeMillis" : 64,
"counts" : {
"input" : 7,
"emit" : 7,
"reduce" : 0,
"output" : 7
},
"ok" : 1
}
>
>
> db.collection.find();
{ "_id" : "bird", "value" : { "count" : 1 } }
{ "_id" : "horse", "value" : { "count" : 1 } }
{ "_id" : "ketty", "value" : { "count" : 1 } }
{ "_id" : "lidongqing", "value" : { "count" : 1 } }
{ "_id" : "liumengyang", "value" : { "count" : 1 } }
{ "_id" : "monkey", "value" : { "count" : 1 } }
{ "_id" : "xiaowanzi", "value" : { "count" : 1 } }
>
>
>