Source:
public DBCursor getComplianceOnDate(Date targetDate, List<String> consumerIds, List<String> owners, List<String> statusFilers) { BasicDBObjectBuilder queryBuilder = BasicDBObjectBuilder.start(); queryBuilder.add("consumer.uuid", new BasicDBObject("$in", consumerCurator.getUuidsOnDate(targetDate, owners, consumerIds))); if (owners != null && !owners.isEmpty()) { queryBuilder.add("consumer.owner.key", new BasicDBObject("$in", owners)); } queryBuilder.add("status.date", new BasicDBObject("$lte", targetDate)); StringBuffer m = new StringBuffer(); m.append("function () {"); m.append(" emit(this.consumer.uuid, {'id': this._id, 'date': this.status.date});"); m.append("}"); StringBuffer r = new StringBuffer(); r.append("function (consumerUuid, statusInfo) {"); r.append(" statusInfo.sort(function(s1, s2) { return s1.date < s2.date });"); r.append(" return statusInfo[0];"); r.append("}"); MapReduceCommand command = new MapReduceCommand(collection, m.toString(), r.toString(), null, MapReduceCommand.OutputType.INLINE, queryBuilder.get()); List<ObjectId> ids = new LinkedList<ObjectId>(); MapReduceOutput output = collection.mapReduce(command); for (DBObject row : output.results()) { DBObject value = (DBObject) row.get("value"); ids.add((ObjectId) value.get("id")); } BasicDBObjectBuilder filterQueryBuilder = BasicDBObjectBuilder.start(); filterQueryBuilder.add("_id", new BasicDBObject("$in", ids)); if (statusFilers != null && !statusFilers.isEmpty()) { filterQueryBuilder.add("status.status", new BasicDBObject("$in", statusFilers)); } DBCursor all = collection.find(filterQueryBuilder.get()); return all; }

Target:
public DBCursor getComplianceOnDate(Date targetDate, List<String> consumerIds, List<String> owners, List<String> statusFilers) { BasicDBObjectBuilder queryBuilder = BasicDBObjectBuilder.start(); queryBuilder.add("consumer.uuid", new BasicDBObject("$in", consumerCurator.getUuidsOnDate(targetDate, owners, consumerIds))); if (owners != null && !owners.isEmpty()) { queryBuilder.add("consumer.owner.key", new BasicDBObject("$in", owners)); } queryBuilder.add("status.date", new BasicDBObject("$lte", targetDate)); String mapFunction = "function () {" + " emit(this.consumer.uuid, {'id': this._id, 'date': this.status.date});" + "}"; String reduceFunction = "function (consumerUuid, statusInfo) {" + " var selected = null;" + " for (var i = 0; i < statusInfo.length; i++) {" + " var status = statusInfo[i];" + " if (selected == null || status.date > selected.date) { selected = status; }" + " }" + " return selected;" + "}"; MapReduceCommand command = new MapReduceCommand(collection, mapFunction, reduceFunction, null, MapReduceCommand.OutputType.INLINE, queryBuilder.get()); List<ObjectId> ids = new LinkedList<ObjectId>(); MapReduceOutput output = collection.mapReduce(command); for (DBObject row : output.results()) { DBObject value = (DBObject) row.get("value"); ids.add((ObjectId) value.get("id")); } BasicDBObjectBuilder filterQueryBuilder = BasicDBObjectBuilder.start(); filterQueryBuilder.add("_id", new BasicDBObject("$in", ids)); if (statusFilers != null && !statusFilers.isEmpty()) { filterQueryBuilder.add("status.status", new BasicDBObject("$in", statusFilers)); } DBCursor all = collection.find(filterQueryBuilder.get()); return all; }