Jelle De Loecker
Jelle De Loecker

Reputation: 21985

Does mongodb's eval() lock the database or not?

According to the documentation:

By default, eval takes a global write lock before evaluating the
JavaScript function.

But you can pass an option to disable this write lock:

Set nolock to true on the eval command to prevent the eval command
from taking the global write lock before evaluating the JavaScript.

But then I see this warning:

Do not use eval for long running operations as eval blocks all
other operations. Consider using other server side code execution options.

So, what is it? Does eval() halt every other operation from running or not?

And if it does: why would you be able to disable the "global write lock", as nothing else can run anyway?

Upvotes: 0

Views: 248

Answers (1)

idbentley
idbentley

Reputation: 4218

By default eval() takes the global write lock, blocking all other operations. If however you provide the nolock it will not take the write lock, so it won't block all other operations. However, even with nolock provided, the global write lock may be taken by the code within the eval block.

Overall, it is not recommended to use eval(), it doesn't work with sharding, and in recent versions of the server it can only be run by admins.

Upvotes: 2

Related Questions