Example usage for com.mongodb.client MapReduceIterable collectionName

List of usage examples for com.mongodb.client MapReduceIterable collectionName

Introduction

In this page you can find the example usage for com.mongodb.client MapReduceIterable collectionName.

Prototype

MapReduceIterable<TResult> collectionName(String collectionName);

Source Link

Document

Sets the collectionName for the output of the MapReduce

The default action is replace the collection if it exists, to change this use #action .

Usage

From source file:com.bluedragon.mongo.MongoCollectionMapReduce.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    String map = getNamedStringParam(argStruct, "map", null);
    if (map == null)
        throwException(_session, "please specify a map");

    String reduce = getNamedStringParam(argStruct, "reduce", null);
    if (reduce == null)
        throwException(_session, "please specify a reduce");

    String outputcollection = getNamedStringParam(argStruct, "outputcollection", null);
    if (outputcollection == null)
        throwException(_session, "please specify a outputcollection");

    String action = getNamedStringParam(argStruct, "type", "replace").toLowerCase();
    String finalize = getNamedStringParam(argStruct, "finalize", null);
    cfData query = getNamedParam(argStruct, "query", null);

    try {//from w w  w  . j  av  a  2s.  c  om
        MapReduceIterable<Document> mi = db.getCollection(collection).mapReduce(map, reduce);

        if (query != null)
            mi.filter(getDocument(query));

        if (finalize != null)
            mi.finalizeFunction(finalize);

        mi.collectionName(outputcollection);
        mi.action(MapReduceAction.valueOf(action));

        // Kick start the map reduce
        mi.first();

        return cfBooleanData.TRUE;

    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}