我想调用在db.runCommand()中创建的Custom函数。
我已经在功能下方创建了:
db.system.js.save(
{
_id : "myAddFunction" ,
value : function (x, y){ return x + y; }
}
);
现在,我想在db.runCommand()中调用该函数: 我尝试过如下操作:
db.runCommand({
aggregate: "mycollection",
pipeline: [
{
$project: {
"X": 1,
"Y": 1,
//"SUM1" : { $sum: ["$X", "$Y"] },
//"SUM2" : { "$myAddFunction": ["$X", "$Y"]},
"SUM3": {
"%function": {
"name": "myAddFunction",
"arguments": [
"$X",
"$Y"
]
}
}
}
},
],
cursor: {}
})
我可以在runCommand下面获取输出,但是如何从myCollection获取Projection?
db.runCommand( {
eval: 'function(u1,u2) {return myAddFunction(u1,u2)};',
args: [ 1,2 ],
nolock: true
}
);