Search in sources :

Example 1 with GroupByResults

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);
}
Also used : GroupByResults(org.springframework.data.mongodb.core.mapreduce.GroupByResults) AggregateIterable(com.mongodb.client.AggregateIterable) DistinctIterable(com.mongodb.client.DistinctIterable) MongoIterable(com.mongodb.client.MongoIterable) MapReduceIterable(com.mongodb.client.MapReduceIterable) FindIterable(com.mongodb.client.FindIterable) Document(org.bson.Document)

Aggregations

AggregateIterable (com.mongodb.client.AggregateIterable)1 DistinctIterable (com.mongodb.client.DistinctIterable)1 FindIterable (com.mongodb.client.FindIterable)1 MapReduceIterable (com.mongodb.client.MapReduceIterable)1 MongoIterable (com.mongodb.client.MongoIterable)1 Document (org.bson.Document)1 GroupByResults (org.springframework.data.mongodb.core.mapreduce.GroupByResults)1