Search in sources :

Example 1 with MapReduceToCollectionOperation

use of com.mongodb.operation.MapReduceToCollectionOperation in project mongo-java-driver by mongodb.

the class MapReduceIterableImpl method execute.

MongoIterable<TResult> execute() {
    if (inline) {
        MapReduceWithInlineResultsOperation<TResult> operation = new MapReduceWithInlineResultsOperation<TResult>(namespace, new BsonJavaScript(mapFunction), new BsonJavaScript(reduceFunction), codecRegistry.get(resultClass)).filter(toBsonDocument(filter)).limit(limit).maxTime(maxTimeMS, MILLISECONDS).jsMode(jsMode).scope(toBsonDocument(scope)).sort(toBsonDocument(sort)).verbose(verbose).readConcern(readConcern).collation(collation);
        if (finalizeFunction != null) {
            operation.finalizeFunction(new BsonJavaScript(finalizeFunction));
        }
        return new OperationIterable<TResult>(operation, readPreference, executor);
    } else {
        MapReduceToCollectionOperation operation = createMapReduceToCollectionOperation();
        String dbName = databaseName != null ? databaseName : namespace.getDatabaseName();
        MongoIterable<TResult> delegated = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(dbName, collectionName), documentClass, resultClass, codecRegistry, primary(), readConcern, executor, new BsonDocument(), new FindOptions().collation(collation)).batchSize(batchSize);
        return new AwaitingWriteOperationIterable<TResult, MapReduceStatistics>(operation, executor, delegated);
    }
}
Also used : FindOptions(com.mongodb.client.model.FindOptions) MongoNamespace(com.mongodb.MongoNamespace) BsonJavaScript(org.bson.BsonJavaScript) MapReduceToCollectionOperation(com.mongodb.operation.MapReduceToCollectionOperation) BsonDocument(org.bson.BsonDocument)

Example 2 with MapReduceToCollectionOperation

use of com.mongodb.operation.MapReduceToCollectionOperation in project mongo-java-driver by mongodb.

the class DBCollection method mapReduce.

/**
     * Allows you to run map-reduce aggregation operations over a collection.
     *
     * @param command specifies the details of the Map Reduce operation to perform
     * @return a MapReduceOutput containing the results of the map reduce operation
     * @mongodb.driver.manual core/map-reduce/ Map-Reduce
     */
public MapReduceOutput mapReduce(final MapReduceCommand command) {
    ReadPreference readPreference = command.getReadPreference() == null ? getReadPreference() : command.getReadPreference();
    if (command.getOutputType() == MapReduceCommand.OutputType.INLINE) {
        MapReduceWithInlineResultsOperation<DBObject> operation = new MapReduceWithInlineResultsOperation<DBObject>(getNamespace(), new BsonJavaScript(command.getMap()), new BsonJavaScript(command.getReduce()), getDefaultDBObjectCodec()).readConcern(getReadConcern()).filter(wrapAllowNull(command.getQuery())).limit(command.getLimit()).maxTime(command.getMaxTime(MILLISECONDS), MILLISECONDS).jsMode(command.getJsMode() == null ? false : command.getJsMode()).sort(wrapAllowNull(command.getSort())).verbose(command.isVerbose()).collation(command.getCollation());
        if (command.getScope() != null) {
            operation.scope(wrap(new BasicDBObject(command.getScope())));
        }
        if (command.getFinalize() != null) {
            operation.finalizeFunction(new BsonJavaScript(command.getFinalize()));
        }
        MapReduceBatchCursor<DBObject> executionResult = executor.execute(operation, readPreference);
        return new MapReduceOutput(command.toDBObject(), executionResult);
    } else {
        String action;
        switch(command.getOutputType()) {
            case REPLACE:
                action = "replace";
                break;
            case MERGE:
                action = "merge";
                break;
            case REDUCE:
                action = "reduce";
                break;
            default:
                throw new IllegalArgumentException("Unexpected output type");
        }
        MapReduceToCollectionOperation operation = new MapReduceToCollectionOperation(getNamespace(), new BsonJavaScript(command.getMap()), new BsonJavaScript(command.getReduce()), command.getOutputTarget(), getWriteConcern()).filter(wrapAllowNull(command.getQuery())).limit(command.getLimit()).maxTime(command.getMaxTime(MILLISECONDS), MILLISECONDS).jsMode(command.getJsMode() == null ? false : command.getJsMode()).sort(wrapAllowNull(command.getSort())).verbose(command.isVerbose()).action(action).databaseName(command.getOutputDB()).bypassDocumentValidation(command.getBypassDocumentValidation()).collation(command.getCollation());
        if (command.getScope() != null) {
            operation.scope(wrap(new BasicDBObject(command.getScope())));
        }
        if (command.getFinalize() != null) {
            operation.finalizeFunction(new BsonJavaScript(command.getFinalize()));
        }
        try {
            MapReduceStatistics mapReduceStatistics = executor.execute(operation);
            DBCollection mapReduceOutputCollection = getMapReduceOutputCollection(command);
            DBCursor executionResult = mapReduceOutputCollection.find();
            return new MapReduceOutput(command.toDBObject(), executionResult, mapReduceStatistics, mapReduceOutputCollection);
        } catch (MongoWriteConcernException e) {
            throw createWriteConcernException(e);
        }
    }
}
Also used : MapReduceStatistics(com.mongodb.operation.MapReduceStatistics) MapReduceWithInlineResultsOperation(com.mongodb.operation.MapReduceWithInlineResultsOperation) BsonString(org.bson.BsonString) BsonJavaScript(org.bson.BsonJavaScript) MapReduceToCollectionOperation(com.mongodb.operation.MapReduceToCollectionOperation)

Aggregations

MapReduceToCollectionOperation (com.mongodb.operation.MapReduceToCollectionOperation)2 BsonJavaScript (org.bson.BsonJavaScript)2 MongoNamespace (com.mongodb.MongoNamespace)1 FindOptions (com.mongodb.client.model.FindOptions)1 MapReduceStatistics (com.mongodb.operation.MapReduceStatistics)1 MapReduceWithInlineResultsOperation (com.mongodb.operation.MapReduceWithInlineResultsOperation)1 BsonDocument (org.bson.BsonDocument)1 BsonString (org.bson.BsonString)1