2011-05-05 96 views
1

如何在mongos mapreduce中指定条件,如同在mongos组函数中所做的一样。在mongodb mapreduce中的条件

我的数据是一样

{lid:1000, age:23}, {lid:3000, age:23}, {lid:1000, age:24}. 

我想只发出盖子的具有1000 emit(this.lid, this.age)值。但是这会发出所有的值。我想在这里有一个条件。地图缩小有什么方法吗?我试图在reduce函数中使用if条件进行过滤,但它不起作用

+0

请提供代码,而不是口头描述,实际上并不能说明您实际尝试过的内容。 – 2011-05-05 03:58:24

回答

1

您的映射函数是一个javascript函数。你可以让它做任何你想要的,如:

if (this.lid == 1000) emit(whatever); 
+0

使用'query'参数比较好,可以使用一些索引来提高性能。 – 2011-06-29 09:29:43

9

你可以在query参数中做到这一点。来自文档页面:http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-Overview

db.runCommand(
{ mapreduce : <collection>, 
    map : <mapfunction>, 
    reduce : <reducefunction> 

    --> [, query : <query filter object>] <-- 

    [, sort : <sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces>] 
    [, limit : <number of objects to return from collection>] 
    [, out : <see output options below>] 
    [, keeptemp: <true|false>] 
    [, finalize : <finalizefunction>] 
    [, scope : <object where fields go into javascript global scope >] 
    [, verbose : true] 
} 
);