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

时间:2023-03-09 02:13:20
MongoDB自定义函数部分  定义及引用

1、

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

2、

//使用Sum函数
db.loadServerScripts();Sum(,[,,,,,,,,])
//在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 : }});