MongoDB:Mapreduce:还不支持reduce-> multiple [英] MongoDB: Mapreduce: reduce->multiple not supported yet

查看:178
本文介绍了MongoDB:Mapreduce:还不支持reduce-> multiple的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MongoDB集合(名为目录"),其中包含约5个天文目录.这些目录中有几个相互引用,因此其中一个文档可能看起来像这样:

I have a MongoDB collection (named "catalog") containing about 5 astronomical catalogs. Several of these catalogs reference each other, so one of the the documents might look something like this:

{ "_id" : ObjectId("4ec574a68e4e7a519166015f"), "bii" : 20.9519, "class" : 2480, "cpdname" : "CPD -21 6109", "decdeg" : -21.8417, "decpm" : 0.004, "dmname" : "-21 4299", "hdname" : "HD 145612", "lii" : 352.8556, "name" : "PPM 265262", "ppmname" : "PPM 265262", "radeg" : 243.2005, "rapm" : 0.0012, "vmag" : 9.6, "xref" : [ ] }

我想做的是使用mapreduce将"hdname","ppmname"等字段移动到外部参照数组中(然后取消设置).

What I want to do is use mapreduce to move the fields such as "hdname","ppmname", etc into the xref array (then unset them).

因此,我尝试一次从hdname字段开始执行此操作.这是map和reduce函数:

So I try to do this one at a time, starting with the hdname field. Here are the map and reduce functions:

map = function() {
    for (var hdname in this.hdname) {
        emit(this._id,this.hdname);
    }
}


reduce = function(key, values) {
    var result = [];
    for (var hdname in values) {
        result.push(hdname);
    }
    return result;
}

我尝试在mongo shell中运行以下命令:

I try running the following command in the mongo shell:

db.catalog.mapReduce(map, reduce,"catalog2");

不幸的是,我收到以下错误:

Unfortunately, I get the following error:

Thu Nov 17 15:52:17 uncaught exception: map reduce failed:{
    "assertion" : "reduce -> multiple not supported yet",
    "assertionCode" : 10075,
    "errmsg" : "db assertion failure",
    "ok" : 0
}

显然我是新手...任何人都可以帮忙吗?

Obviously I'm a newbie... can anyone help?

Jason

推荐答案

文档说当前,reduce函数的返回值不能是数组(通常是对象或数字)."

因此,请创建一个对象,然后在其中包装您的数组.还请确保reduce的输出与输入类型相同,因此您需要在map操作中发出相似的值.

So create an object instead and wrap your array in that. Make sure also that the output of reduce is the same as the input type, so you'll need to emit a similar value in the map operation.

但是...为什么要使用Map-Reduce来做到这一点?如果发出_id值,则没有什么可减少的,因为每个键都是唯一的.为什么不只是遍历集合,复制值并逐个更新每个记录?

BUT ... why use Map-Reduce to do this? If you emit the _id value there's nothing to reduce as each key will be unique. Why not just iterate over the collection copying the values and updating each record one by one?

这篇关于MongoDB:Mapreduce:还不支持reduce-> multiple的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆