3

After starting a MongoDB find or aggregate operation, I want to be able to cancel it. Sometimes the user wants to cancel a long running operation and move on to something else.

I know that it's possible to use db.killOp() to stop a running operation, but there doesn't seem to be a good way to determine which operation is the one to kill. The accepted form is to use db.currentOp() to find the operation that is from the same client and has the same operation. But since this is a web application, all operations will appear to come from the same "client", and it's possible that two different users will trigger the same operation. I don't want to inadvertently kill the wrong one if one user decides cancel.

Each operation returned by currentOp() has a unique opid, but I don't see a way to map the opid to an operation started from the Java API. In the synchronous portion of the Java API, of course there's no information until the find or aggregate is finished. But even in the async API, there is only a callback for results.

I checked the other fields returned by currentOp(), and there doesn't seem to be a way to put my own marker or counter on each query so that I can associate them after the fact. How can I use the Java API to allow users to cancel/kill a MongoDB find or aggregate operation after the operation has started?

1 Answer 1

3

There currently isn't an API option to kill jobs via the Java driver. There's an open issue against this, however. As mentioned in the comments you might explore the use of maxTime as a means of terminating an aggregation that takes too long to finish.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the link to the open issue. I can't use the timeout because the user may be willing to wait for the query to complete, or they may not be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.