Mongodb map reduce 结果格式更改
Mongodb map reduce result format changes
我正在使用 java api 并调用以下 map reduce 命令:
MAP
function() {
emit('nogroup',{endtime: this.endtime, _owner: this._owner});
}
REDUCE
function Reduce(key, values) {
var res = 3426533531976;
for ( var i=0; i<values.length; i++ ) {
if ( values[i].endtime < res && values[i]._owner == null )
res = values[i].endtime;
}
return res;
}
使用此代码
String map = "function() { emit('nogroup',{endtime: this.endtime, _owner: this._owner}); }";
String reduce = "function Reduce(key, values) { var res = 9426533531976; for ( var i=0; i<values.length; i++ ) { if ( values[i].endtime < res && values[i]._owner == null ) res = values[i].endtime; } return res;}";
MapReduceCommand cmd = new MapReduceCommand(userdataCollection, map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
MapReduceOutput out = userdataCollection.mapReduce(cmd);
有时结果看起来像这样
{ "_id" : "nogroup" , "value" : { "floatApprox" : 1.426628475177E12}}
有时像这样
{ "_id" : "nogroup" , "value" : 9.426533531976E12}
如何始终使用相同的代码提取长结果?
目前我有
for (DBObject a : out.results()) {
return ((BasicDBObject) a.get("value")).getLong("floatApprox");
}
但这仅适用于 floatApprox 变体
只需在:
之后添加一个检查
res = values[i].endtime;
例如
res = values[i].endtime;
if ( typeof res == "object"){
res = res.floatApprox;
}
我正在使用 java api 并调用以下 map reduce 命令:
MAP
function() {
emit('nogroup',{endtime: this.endtime, _owner: this._owner});
}
REDUCE
function Reduce(key, values) {
var res = 3426533531976;
for ( var i=0; i<values.length; i++ ) {
if ( values[i].endtime < res && values[i]._owner == null )
res = values[i].endtime;
}
return res;
}
使用此代码
String map = "function() { emit('nogroup',{endtime: this.endtime, _owner: this._owner}); }";
String reduce = "function Reduce(key, values) { var res = 9426533531976; for ( var i=0; i<values.length; i++ ) { if ( values[i].endtime < res && values[i]._owner == null ) res = values[i].endtime; } return res;}";
MapReduceCommand cmd = new MapReduceCommand(userdataCollection, map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
MapReduceOutput out = userdataCollection.mapReduce(cmd);
有时结果看起来像这样
{ "_id" : "nogroup" , "value" : { "floatApprox" : 1.426628475177E12}}
有时像这样
{ "_id" : "nogroup" , "value" : 9.426533531976E12}
如何始终使用相同的代码提取长结果?
目前我有
for (DBObject a : out.results()) {
return ((BasicDBObject) a.get("value")).getLong("floatApprox");
}
但这仅适用于 floatApprox 变体
只需在:
之后添加一个检查res = values[i].endtime;
例如
res = values[i].endtime;
if ( typeof res == "object"){
res = res.floatApprox;
}