Navigation
This version will reach end of life on Feb 2018. To upgrade, go to the Learn more about upgrading your version of MongoDB.

sleep()

On this page

Definition

sleep(ms)
Parameter Type Description
ms integer A duration in milliseconds.

sleep() suspends a JavaScript execution context for a specified number of milliseconds.

Example

Consider a low-priority bulk data import script. To avoid impacting other processes, you may suspend the shell after inserting each document, distributing the cost of insertion over a longer period of time.

The following example mongo script will load a JSON file containing an array of documents, and save one element every 100 milliseconds.

JSON.parse(cat('users.json')).forEach(function(user) {
    db.users.save(user);
    sleep(100);
});
←   cd() copyDbpath()  →