/**
*
* @param businessNo
* @param beginTime 开始时间
* @param endTime 结束时间
* @param pageNo 页码
* @param pageSize 分页大小
* @return
*/
public static List<DBObject> query(Integer businessNo,Date beginTime,Date endTime,int pageNo,int pageSize){
StringBuffer map = new StringBuffer();
map.append("function(){ ");
map.append(" var t = new Date(this.createTime);");
map.append(" emit({ ");
map.append(" orgId:this.orgId,");
map.append(" operatorId:this.operatorId,");
map.append(" objId:this.businessParam.objId,");
map.append(" creatDate:t.getFullYear() +'-'+(t.getMonth() + 1)+'-'+t.getDate()");
map.append(" },");
map.append(" {count:1}");
map.append(" ); ");
map.append("} ");
log.info(">>>>>>>map:{}",map.toString());
StringBuffer reduce = new StringBuffer();
reduce.append("function(key, values) {");
reduce.append(" var total = 0;");
reduce.append(" for (var i=0; i<values.length; i++) { ");
reduce.append(" total += values[i].count;");
reduce.append(" } ");
reduce.append(" return {count: total}; ");
reduce.append("} ");
log.info(">>>>>>>reduce:{}",reduce.toString());
StringBuffer finalize = new StringBuffer();
finalize.append("function(key, rvalues) { ");
finalize.append(" var sum = 0; ");
finalize.append(" if(rvalues.count>0){ ");
finalize.append(" rvalues.get='true'; ");
finalize.append(" }else{ ");
finalize.append(" rvalues.get='false'");
finalize.append(" } ");
finalize.append(" return rvalues; ");
finalize.append("}");
log.info(">>>>>>>finalize:{}",finalize.toString());
//查询条件
DBObject query = new BasicDBObject();
query.put("businessNo", businessNo);
query.put("operatorId", new BasicDBObject("$exists", true).append("$ne", 0));
query.put("createTime", new BasicDBObject("$gte", beginTime.getTime()).append("$lte", endTime.getTime()));
query.put("businessParam.objId", new BasicDBObject("$exists", true));
DBObject command = new BasicDBObject();
command.put("mapreduce", "MyLogs");
command.put("query", query);
command.put("map", map.toString());
command.put("reduce", reduce.toString());
command.put("out", "MapReduceCommand.OutputType.INLINE");
command.put("verbose", true);
DBCollection db = MongoDbInit.getDb().getCollection("MyLogs");
command.put("finalize", finalize.toString());
MapReduceOutput out = db.mapReduce(command);
DBObject having = new BasicDBObject();
having.put("value.get", "true");
DBObject orderBy = new BasicDBObject();
orderBy.put("createTime", -1);//按createTime倒序排
int skip = (pageNo-1)*pageSize;
List<DBObject> resultList = new ArrayList();
resultList = out.getOutputCollection().find(having).sort(orderBy).skip(skip).limit(pageSize).toArray();
return resultList;
}
数据结构
{
"_id" : ObjectId("52f9c392b7603fe75cff49e7"),
"deviceId" : "ffffffff-d0b9-867d-191b-921900000000",
"operationTime" : NumberLong("1392100740000"),
"operatorType" : 1,
"operatorId" : 3587,
"platform" : "android",
"businessStatus" : 1,
"sessionId" : "",
"orgId" : 104,
"businessName" : "活动详情",
"createTime" : NumberLong("1392100242676"),
"businessParam" : {
"objId" : 653,
"time" : 715,
"objType" : 1
},
"businessNo" : 1101
}