Question

I'm very new to the MongoDB world and I'm connecting to a MongoDB server (2.6) using the Java driver (2.12).

Is there anyway to cancel a MapReduceCommand (or an ongoing .aggregate() operation) ? I do not find anything in the documentation.

Was it helpful?

Solution

I'm not sure if there are direct API methods either, but like most mongo operations these really just function by making similar wire protocol operations to "system collections" as it were.

So the methods for finding and cancelling are documented under db.currentOp() and db.killOp(). Those these are the shell implementation methods all they are doing id accessing the "system collections" underneath, being $cmd.sys.inprog and $cmd.sys.killop respectively.

Specifically their shell implementations are:

currentOp:

function ( arg ){
    var q = {}
    if ( arg ) {
        if ( typeof( arg ) == "object" )
        Object.extend( q , arg );
        else if ( arg )
        q["$all"] = true;
    }
    return this.$cmd.sys.inprog.findOne( q );
}

killOp:

function (op) {
    if( !op )
        throw "no opNum to kill specified";
    return this.$cmd.sys.killop.findOne({'op':op});
}

This makes a simple translation in to Java code, so for instance, after determining the current operation number for the operation you wish to halt, you just issue a query as follows ( forgiving my quick cut and paste from existing Sprint data code, so whatever actual method to get the collection object ):

DBCollection collection = mongoOperation.getCollection("$cmd.sys.killop");

BasicDBObject query = new BasicDBObject("op", opNumber);
DBObject result = collection.findOne(query);

That's the basics of it. See the documentation resources listed for more information on how to match which operation you want to cancel.

OTHER TIPS

In addition to previous answer (which is correct), here is a way how you can disallow your mapreduce (or actually any other command) to run more than specific amount of time.

I found this to be really handy, because it allows you to be proactive. Note that this is only available only after 2.6. For example here I am telling to kill my find query after 0.2 secs.

db.collection.find({
  // my query
}).maxTimeMS(200)

Look at maxTimeMS for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top