String map = "function () {"
+ " var key = this.custNo; "
+ " var value = {custNo: this.custNo,loanNo:this.loanNo,"
+ " list:[{"
+ " certNo:this.list.certNo,"
+ " phoneNo:this.list.phoneNo,"
+ " phoneName:this.list.phoneName,"
+ " othMsg:this.list.othMsg"
+ " }]"
+ " };"
+ " emit(key, value);"
+ "}";
// reduce 函数
String reduce = "function(key, values) {"
+ " var result = {custNo:key,loanNo:null,list:[]};"
+ " values.forEach( function(value) {"
+ " result.list = result.list.concat(value.list);"
+ " });"
+ " result.loanNo = values[0].loanNo;"
+ " return result;"
+ "}";
BasicDBObject query = new BasicDBObject();
MapReduceCommand cmd = new MapReduceCommand(calllists, map, reduce,
"out_user", MapReduceCommand.OutputType.REPLACE, query);// out_user是指定的输出数据的集合
MapReduceOutput out = calllists.mapReduce(cmd);
Object [] str_array = null; //通过数组读取jsonstr
String jsonStr = null; //保存最终得到的json格式
for (DBObject o : out.results()) {
str_array = o.toMap().values().toArray();
jsonStr = str_array[1].toString();
}
最后得到的JSON格式如下:
{
"custNo": "456654",
"loanNo": "654321",
"list": [
{
"certNo": "130406199302073017",
"phoneNo": "18583738107",
"phoneName": "duxin",
"othMsg":"jjj"
},
{
"certNo": "654321",
"phoneNo": "13256565544",
"phoneName": "haha",
"othMsg":"hahaha"
}
]
}
————————————————
版权声明:本文为CSDN博主「艾慕达」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a18716374124/article/details/74101187