Mongodb Mongo映射减少错误
作为MongoDB的新手,我正在尝试使用MapReduce。 不幸的是,我得到了以下错误:Mongodb Mongo映射减少错误,mongodb,mapreduce,Mongodb,Mapreduce,作为MongoDB的新手,我正在尝试使用MapReduce。 不幸的是,我得到了以下错误: mongos> db.events.mapReduce(m,r,"errors"); Fri Apr 12 11:39:15.637 JavaScript execution failed: map reduce failed:{ "ok" : 0, "errmsg" : "MR parallel processing failed: { errmsg: \"exception: r
mongos> db.events.mapReduce(m,r,"errors");
Fri Apr 12 11:39:15.637 JavaScript execution failed: map reduce failed:{
"ok" : 0,
"errmsg" : "MR parallel processing failed: { errmsg: \"exception: reduce -> multiple not supported yet\", code: 10075, ok: 0.0 }"
} at src/mongo/shell/collection.js:L970
mongos>
我正在使用Mongo2.4.0
我的事件集合如下所示:
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "I", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.236Z"), "class" : "E", "text" : "error message" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.239Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "S", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "I", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.173Z"), "class" : "E", "text" : "another error message" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.188Z"), "class" : "F", "service" : "service2" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "I", "service" : "service1" }
{ ""sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.853Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.852Z"), "class" : "I", "service" : "service1" }
var m = function() {
array = [];
if (this.class == "E") {
value = { time: this.time, error: this.text };
array.push(value);
emit(this.sid, {a:array});
} else if (this.class == "F") {
value = { service: this.service };
array.push(value);
emit(this.sid, {a:array});
}
}
var r = function(key,values) {
result = {a:[]};
values.forEach(function (v) {
result.a = v.a.concat(result.a);
});
return result;
}
db.events.mapReduce(m,r,"errors");
> db.errors.find().pretty()
{
"_id" : "1UADM45MCGRTW ",
"value" : {
"a" : [
{
"service" : "service1"
},
{
"time" : ISODate("2013-04-08T11:33:13.236Z"),
"error" : "error message"
}
]
}
}
{
"_id" : "1UDO3H7YUOK08 ",
"value" : {
"a" : [
{
"service" : "service2"
},
{
"time" : ISODate("2013-04-08T11:33:08.173Z"),
"error" : "another error message"
}
]
}
}
{
"_id" : "1UDO3JVXIS2UZ ",
"value" : {
"a" : [
{
"service" : "service1"
}
]
}
}
我的意图是获取所有“E”条目,并将它们与“F”条目的对应“sid”的“service”字段相结合。所以这应该是非常简单的,但我无法让它运行(因为上面的错误)
我使用的map/reduce函数有:
var m = function() {
if (this.class == "E") {
value = { time: this.time, error: this.text };
emit(this.sid, value);
} else if (this.class == "F") {
value = { service: this.service };
emit(this.sid, value);
}
}
var r = function(key,values) {
return values;
}
db.events.mapReduce(m,r,"errors");
根据文件() “reduce函数必须返回一个对象,该对象的类型必须与map函数发出的值的类型相同”
当您发射一个对象时,reduce也应该发射一个对象-因此将
值
包装到一个对象中,您就可以开始了。根据文档()
“reduce函数必须返回一个对象,该对象的类型必须与map函数发出的值的类型相同”
当你发射一个对象时,reduce也应该发射一个对象-因此在一个对象中包装
值
,你就可以开始了。问题是你想为每个键积累所有错误,这意味着你必须发射一个文档数组,然后reduce必须将它们组合成一个数组(因为它将接收一组数组)
另一个问题是,您希望将每个唯一cid的不同类组合成一些组合的简化结果,但reduce函数甚至没有尝试这样做
很难知道每个sid可能会得到多少不同的类值,但如果它与您提供的示例数据类似,我猜您希望得到如下结果:
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "I", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.236Z"), "class" : "E", "text" : "error message" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.239Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "S", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "I", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.173Z"), "class" : "E", "text" : "another error message" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.188Z"), "class" : "F", "service" : "service2" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "I", "service" : "service1" }
{ ""sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.853Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.852Z"), "class" : "I", "service" : "service1" }
var m = function() {
array = [];
if (this.class == "E") {
value = { time: this.time, error: this.text };
array.push(value);
emit(this.sid, {a:array});
} else if (this.class == "F") {
value = { service: this.service };
array.push(value);
emit(this.sid, {a:array});
}
}
var r = function(key,values) {
result = {a:[]};
values.forEach(function (v) {
result.a = v.a.concat(result.a);
});
return result;
}
db.events.mapReduce(m,r,"errors");
> db.errors.find().pretty()
{
"_id" : "1UADM45MCGRTW ",
"value" : {
"a" : [
{
"service" : "service1"
},
{
"time" : ISODate("2013-04-08T11:33:13.236Z"),
"error" : "error message"
}
]
}
}
{
"_id" : "1UDO3H7YUOK08 ",
"value" : {
"a" : [
{
"service" : "service2"
},
{
"time" : ISODate("2013-04-08T11:33:08.173Z"),
"error" : "another error message"
}
]
}
}
{
"_id" : "1UDO3JVXIS2UZ ",
"value" : {
"a" : [
{
"service" : "service1"
}
]
}
}
在您的示例中,生成的集合如下所示:
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "I", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.236Z"), "class" : "E", "text" : "error message" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.239Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "S", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "I", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.173Z"), "class" : "E", "text" : "another error message" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.188Z"), "class" : "F", "service" : "service2" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "I", "service" : "service1" }
{ ""sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.853Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.852Z"), "class" : "I", "service" : "service1" }
var m = function() {
array = [];
if (this.class == "E") {
value = { time: this.time, error: this.text };
array.push(value);
emit(this.sid, {a:array});
} else if (this.class == "F") {
value = { service: this.service };
array.push(value);
emit(this.sid, {a:array});
}
}
var r = function(key,values) {
result = {a:[]};
values.forEach(function (v) {
result.a = v.a.concat(result.a);
});
return result;
}
db.events.mapReduce(m,r,"errors");
> db.errors.find().pretty()
{
"_id" : "1UADM45MCGRTW ",
"value" : {
"a" : [
{
"service" : "service1"
},
{
"time" : ISODate("2013-04-08T11:33:13.236Z"),
"error" : "error message"
}
]
}
}
{
"_id" : "1UDO3H7YUOK08 ",
"value" : {
"a" : [
{
"service" : "service2"
},
{
"time" : ISODate("2013-04-08T11:33:08.173Z"),
"error" : "another error message"
}
]
}
}
{
"_id" : "1UDO3JVXIS2UZ ",
"value" : {
"a" : [
{
"service" : "service1"
}
]
}
}
您可能可以添加一个finalize函数,将此数组转换为单个文档的结果,但这取决于您想要的结果集合的格式以及每个sid的预期输入。问题在于,您想要累积每个键的所有错误,这意味着您必须发出一个文档,然后reduce必须将它们合并到一个数组中(因为它将接收一个数组数组) 另一个问题是,您希望将每个唯一cid的不同类组合成一些组合的简化结果,但reduce函数甚至没有尝试这样做 很难知道每个sid可能会得到多少不同的类值,但如果它与您提供的示例数据类似,我猜您希望得到如下结果:
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "I", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.236Z"), "class" : "E", "text" : "error message" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.239Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "S", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "I", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.173Z"), "class" : "E", "text" : "another error message" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.188Z"), "class" : "F", "service" : "service2" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "I", "service" : "service1" }
{ ""sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.853Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.852Z"), "class" : "I", "service" : "service1" }
var m = function() {
array = [];
if (this.class == "E") {
value = { time: this.time, error: this.text };
array.push(value);
emit(this.sid, {a:array});
} else if (this.class == "F") {
value = { service: this.service };
array.push(value);
emit(this.sid, {a:array});
}
}
var r = function(key,values) {
result = {a:[]};
values.forEach(function (v) {
result.a = v.a.concat(result.a);
});
return result;
}
db.events.mapReduce(m,r,"errors");
> db.errors.find().pretty()
{
"_id" : "1UADM45MCGRTW ",
"value" : {
"a" : [
{
"service" : "service1"
},
{
"time" : ISODate("2013-04-08T11:33:13.236Z"),
"error" : "error message"
}
]
}
}
{
"_id" : "1UDO3H7YUOK08 ",
"value" : {
"a" : [
{
"service" : "service2"
},
{
"time" : ISODate("2013-04-08T11:33:08.173Z"),
"error" : "another error message"
}
]
}
}
{
"_id" : "1UDO3JVXIS2UZ ",
"value" : {
"a" : [
{
"service" : "service1"
}
]
}
}
在您的示例中,生成的集合如下所示:
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.229Z"), "class" : "I", "service" : "service1" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.236Z"), "class" : "E", "text" : "error message" }
{ "sid" : "1UADM45MCGRTW ", "time" : ISODate("2013-04-08T11:33:13.239Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "S", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.095Z"), "class" : "I", "service" : "service2" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.173Z"), "class" : "E", "text" : "another error message" }
{ "sid" : "1UDO3H7YUOK08 ", "time" : ISODate("2013-04-08T11:33:08.188Z"), "class" : "F", "service" : "service2" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "I", "service" : "service1" }
{ ""sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:39.480Z"), "class" : "S", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.853Z"), "class" : "F", "service" : "service1" }
{ "sid" : "1UDO3JVXIS2UZ ", "time" : ISODate("2013-04-08T11:28:40.852Z"), "class" : "I", "service" : "service1" }
var m = function() {
array = [];
if (this.class == "E") {
value = { time: this.time, error: this.text };
array.push(value);
emit(this.sid, {a:array});
} else if (this.class == "F") {
value = { service: this.service };
array.push(value);
emit(this.sid, {a:array});
}
}
var r = function(key,values) {
result = {a:[]};
values.forEach(function (v) {
result.a = v.a.concat(result.a);
});
return result;
}
db.events.mapReduce(m,r,"errors");
> db.errors.find().pretty()
{
"_id" : "1UADM45MCGRTW ",
"value" : {
"a" : [
{
"service" : "service1"
},
{
"time" : ISODate("2013-04-08T11:33:13.236Z"),
"error" : "error message"
}
]
}
}
{
"_id" : "1UDO3H7YUOK08 ",
"value" : {
"a" : [
{
"service" : "service2"
},
{
"time" : ISODate("2013-04-08T11:33:08.173Z"),
"error" : "another error message"
}
]
}
}
{
"_id" : "1UDO3JVXIS2UZ ",
"value" : {
"a" : [
{
"service" : "service1"
}
]
}
}
您可能可以添加finalize函数将此数组转换为单个文档的结果,但这取决于您想要的结果集合的格式以及每个sid的预期输入