sleep()
On this page
MongoDB 5.0 is end of life as of October 2024. This version of the documentation is no longer
supported. To upgrade your 5.0 deployment, see the MongoDB 6.0 upgrade procedures.
Note
The native method listed here is for the legacy mongo
shell.
To use native methods in mongosh
, see
Native Methods in mongosh
.
Definition
sleep(ms)
- ParameterTypeDescription
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 mongosh
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); });