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 }});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM