从 2.4 版开始,MongoDB 使用 V8 引擎,因此 ECMA-262 第 5 版。在 shell中运行 MapReduce 作业时,有一个额外支持的功能列表。
如果我使用本机 Node.js 驱动程序运行 mapReduce 作业会怎样?我可以在map
and中使用任何模块或 JavaScript 函数reduce
吗?
编辑:我现在不能做一些测试来弄清楚,但我会尽快更新问题。
从 2.4 版开始,MongoDB 使用 V8 引擎,因此 ECMA-262 第 5 版。在 shell中运行 MapReduce 作业时,有一个额外支持的功能列表。
如果我使用本机 Node.js 驱动程序运行 mapReduce 作业会怎样?我可以在map
and中使用任何模块或 JavaScript 函数reduce
吗?
编辑:我现在不能做一些测试来弄清楚,但我会尽快更新问题。
MapReduce runs within the MongoDB instance.
You can't use any other programming language or platform to create a MapReduce right now. You could simulate the concept I suppose, but it would mean all data was transferred to your application, etc.
The body of the function is transferred from the Node.JS process to MongoDB (and shards, etc.) by calling toString
on the provided map
function (see here).
if ('function' === typeof map) {
map = map.toString();
}
You could embed functions in the map
function, but you'd want to confirm that the performance met your needs as the complexity of the function increases. But, it still would be limited to the function you passed, and could not access other script in Node.JS that might be executing.
Edit:
Also, you could use the scope
parameter of the mapReduce
call to attach additional functions. If for example, you passed a scope
as:
{ add10: function(v) { return v + 10; } }
The function called add10
would be available in the map
call (and also the reduce
and finalize
functions):
var fancyMath = add10(this.total);
So, using that technique, you could pass some limited functionality. It does get serialized as BSON and sent to the MongoDB servers, so it needs to be self-contained (and not reference other JavaScript functions that won't be available locally).