有条件地在 MongoDb 集合上运行 map/reduce
我对map/reduce的理解似乎还不够。我想知道是否可以从集合中选择文档子集并仅在该子集上运行我的映射和归约函数。
一般情况下可以吗?
如果不是,则意味着所有过滤都必须在发出之前在映射函数中完成。我已经为我的用例编写了一个这样的映射:
map = <<-EOF
function(){
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
};
project_ids = ['#{@project_ids.map{|pid| pid.to_s}.join('\',\'')}'];
if(project_ids.contains(this.project_id.toString())) {
if(this.time.getFullYear() == '#{@year}' && this.time.getMonth() == '#{@month.to_i - 1}') {
emit(
this.time.getDate(),
{
foos: this.stats.foos
}
);
}
}
};
EOF
这是 Rails 项目的一部分,使用 map/reduce 实现的方法实际上比纯 ruby 方法慢 3 倍。
所以我想知道是否有可能首先应用某些条件过滤我的记录集,然后通过映射/减少运行记录子集来获取我的数据。
谁能启发我吗? 提前谢谢 菲利克斯
My understanding of map/reduce seems to be insufficient. I wonder if I can select a subset of documents from a collection and run my map and reduce functions only on that subset.
Is that possible in general?
In case not, it meant, that all filtering has to be done in the map function before emitting. I already wrote a map like that for my usecase:
map = <<-EOF
function(){
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
};
project_ids = ['#{@project_ids.map{|pid| pid.to_s}.join('\',\'')}'];
if(project_ids.contains(this.project_id.toString())) {
if(this.time.getFullYear() == '#{@year}' && this.time.getMonth() == '#{@month.to_i - 1}') {
emit(
this.time.getDate(),
{
foos: this.stats.foos
}
);
}
}
};
EOF
This is part of a Rails project and the method implemented using map / reduce is in fact 3 times slower than a pure ruby method.
So I wonder whether there is any possibility to first filter my set of records applying some conditions and afterwards run the subset of records through map / reduce to get my data.
Can anyone enlighten me?
Thx in advance
Felix
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有。 Map/Reduce 函数的参数之一是“查询”,它可以满足您的需求。看看 http://www.mongodb.org/display/DOCS/MapReduce了解详情。
There is. One of the parameters of the map/reduce function is "query" which allows exactly what you need. Have a look at http://www.mongodb.org/display/DOCS/MapReduce for details.
如果您使用 ORM,那么您通常可以从作用域集合中获取选择器并在 map-reduce 中使用它,例如使用 Mongoid:
If you're using an ORM, then you can often obtain the selector from a scoped collection and use that in the map-reduce, e.g. with Mongoid: