使用MongoDB的MapReduce Java驱动程序错误的BSONElement断言类型错误 [英] MapReduce using MongoDB Java Driver failes with wrong type for BSONElement assertion
问题描述
我是MongoDB和MapReduce的新手。我需要在我的数据库中的集合上做一些MapReduce。 MAP
和 REDUCE_MAX
函数有效,因为我能够在Mongo交互式shell中完成我的需求(v.1.8) 0.2)。但是,我尝试使用Mongo Java驱动程序(v.2.6.3)执行相同的操作时出错
I'm pretty new to MongoDB and MapReduce. I need to do some MapReduce on a collection in my DB. The MAP
and REDUCE_MAX
functions work, since I was able to accomplish my needs in the Mongo interactive shell (v.1.8.2). However, I get an error trying to perform the same thing using the Mongo Java Driver (v. 2.6.3)
我的 MAP
和 REDUCE_MAX
函数如下所示:
String MAP =
"function(){" +
"if(this.type != \"checkin\"){return;}" +
"if(!this.venue && !this.venue.id){return;}" +
"emit({userId:this.userId, venueId:this.venue.id}, {count:1});" +
"};";
String REDUCE_MAX =
"function(key, values){" +
"var res = {count:0};" +
"values.forEach(function(value){result.count += value.count;});" +
"return res;" +
"};";
这是我正在执行的命令:
This is the command I'm executing:
MapReduceOutput sum = collection
.mapReduce(MAP, REDUCE_MAX, null, null);
这是我得到的错误:
com.mongodb.CommandResult$CommandFailure: command failed [command failed [mapreduce] { "assertion" : "wrong type for BSONElement (replace) 10 != 2" , "assertionCode" : 13111 , "errmsg" : "db assertion failure" , "ok" : 0.0}
我不知道知道哪个BSONElement的类型错误。我已经用Google搜索了 assertionCode:13111
。我还检查了MongoDB日志,但没有找到任何线索。
I don't know which BSONElement has the wrong type. And I've already googled the assertionCode: 13111
. I've also checked in the MongoDB log, but did not find any clues there.
有没有人有想法,我可能会遗漏/做错什么?如果你们需要更多详细信息,请告诉我们。
Does anyone have an idea, what I could be missing/doing wrong? If you guys need more details, just let me know please.
推荐答案
今天我偶然发现了我的错误并计划分享解决方案这里,以防有人遇到类似的问题。
Today I stumbled upon my mistake and figured to share the solution here, just in case somebody encounters a similar problem.
调用 mapReduce
方法导致了问题:
The invocation of the mapReduce
method was causing the problem:
MapReduceOutput sum = collection
.mapReduce(MAP, REDUCE_MAX, null, null);
查看此方法的Javadoc:
Have a look at the Javadoc for this method:
/**
* performs a map reduce operation
* Runs the command in REPLACE output mode (saves to named collection)
*
* @param map
* map function in javascript code
* @param outputTarget
* optional - leave null if want to use temp collection
* @param reduce
* reduce function in javascript code
* @param query
* to match
* @return
* @throws MongoException
* @dochub mapreduce
*/
它声明该命令是使用 REPLACE $ c执行的$ c>作为输出模式,如果想要临时收集,
outputTarget
应为 null
。
It states that the command is executed using REPLACE
as output mode and that if one wants a temporary collection, the outputTarget
should be null
.
不幸的是,构造函数 MapReduceCommand
,用于 mapReduce
方法,只允许 outputTar如果
可以为空(根据Javadoc的 OutputType
设置为 INLINE
,则 MapReduceCommand.getOutputTarget()
)。
Unfortunately though, the constructorMapReduceCommand
, which is used in the mapReduce
method, only allows the outputTarget
to be nullable if the OutputType
is set to INLINE
(according to the Javadoc of MapReduceCommand.getOutputTarget()
).
所以我所要做的就是从<$更改第三个参数c $ c> null 到某些 String
,如下所示:
So all I had to do is to change the third parameter from null
to some String
, like so:
MapReduceOutput sum = collection
.mapReduce(MAP, REDUCE_MAX, "tmp", null);
这就像我试图找出它为什么没有玩的唯一参数工作。我希望有人可能会觉得这很有用。
This was like the only parameter I had not played around with while trying to figure out why it didn't work. I hope somebody might find this helpful.
这篇关于使用MongoDB的MapReduce Java驱动程序错误的BSONElement断言类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!