【问题标题】:where condition in mongodb mapreducemongodb mapreduce中的条件
【发布时间】:2011-08-18 23:21:11
【问题描述】:

如何在 mongos mapreduce 中指定条件,就像我们在 mongos 组函数中所做的那样。

我的数据是这样的

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

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

【问题讨论】:

  • 请提供代码,而不是口头描述,这并不能说明您实际尝试过的内容。

标签: mongodb pymongo


【解决方案1】:

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

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

【讨论】:

  • 使用query参数更好,可以使用一些索引来提高性能。
【解决方案2】:

您可以在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]
 }
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 2015-09-27
    • 2014-04-29
    • 2013-06-03
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多