Loading

MongoDB自定义函数部分 定义及引用

1、

//定义一个Sum的函数
db.system.js.save({_id:"Sum",
            value:function(key,values)
            {
                var total = 0;
                for(var i =0;i <values.length;i++)
                    total += values[i];
                return total;
                }});

2、

//使用Sum函数
db.loadServerScripts();Sum(1,[1,2,3,4,5,6,7,8,9])
//在People集合中使用Sum函数
db.people.runCommand(
{
    mapreduce :"people",
    map:function(){
            emit(
            {key0:this.name,
            key1:this.created},
            this.phonenum
            );},
        reduce:function(key,values)
        {
            var result = Sum(key, values);
            return result;
            },
        out :{inline : 1 }});

 

posted @ 2016-09-12 10:59  3WLineCode  阅读(8269)  评论(0编辑  收藏  举报