use of org.springframework.data.mongodb.core.mapreduce.GroupByResults in project spring-data-mongodb by spring-projects.
the class MongoTemplate method group.
public <T> GroupByResults<T> group(@Nullable Criteria criteria, String inputCollectionName, GroupBy groupBy, Class<T> entityClass) {
Document document = groupBy.getGroupByObject();
document.put("ns", inputCollectionName);
if (criteria == null) {
document.put("cond", null);
} else {
document.put("cond", queryMapper.getMappedObject(criteria.getCriteriaObject(), Optional.empty()));
}
if (document.containsKey("initial")) {
Object initialObj = document.get("initial");
if (initialObj instanceof String) {
String initialAsString = replaceWithResourceIfNecessary((String) initialObj);
document.put("initial", Document.parse(initialAsString));
}
}
if (document.containsKey("$reduce")) {
document.put("$reduce", replaceWithResourceIfNecessary(ObjectUtils.nullSafeToString(document.get("$reduce"))));
}
if (document.containsKey("$keyf")) {
document.put("$keyf", replaceWithResourceIfNecessary(ObjectUtils.nullSafeToString(document.get("$keyf"))));
}
if (document.containsKey("finalize")) {
document.put("finalize", replaceWithResourceIfNecessary(ObjectUtils.nullSafeToString(document.get("finalize"))));
}
Document commandObject = new Document("group", document);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Executing Group with Document [{}]", serializeToJsonSafely(commandObject));
}
Document commandResult = executeCommand(commandObject, this.readPreference);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Group command result = [{}]", commandResult);
}
@SuppressWarnings("unchecked") Iterable<Document> resultSet = (Iterable<Document>) commandResult.get("retval");
List<T> mappedResults = new ArrayList<T>();
DocumentCallback<T> callback = new ReadDocumentCallback<T>(mongoConverter, entityClass, inputCollectionName);
for (Document resultDocument : resultSet) {
mappedResults.add(callback.doWith(resultDocument));
}
return new GroupByResults<T>(mappedResults, commandResult);
}
Aggregations