Skip to content

DOCS-4412: Document sleep() #2353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions source/includes/apiargs-method-sleep-param.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
arg_name: param
description: |
A duration in milliseconds.
interface: method
name: ms
operation: sleep
optional: false
position: 1
type: integer
...
4 changes: 4 additions & 0 deletions source/includes/ref-toc-method-native.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ name: ":method:`cd()`"
file: /reference/method/cd
description: "Changes the current working directory to the specified path."
---
name: ":method:`sleep()`"
file: /reference/method/sleep
description: "Suspends the :program:`mongo` shell for a given period of time."
---
name: ":method:`copyDbpath()`"
file: /reference/method/copyDbpath
description: "Copies a local :setting:`~storage.dbPath`. For internal use."
Expand Down
32 changes: 32 additions & 0 deletions source/reference/method/sleep.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=======
sleep()
=======

.. default-domain:: mongodb

Definition
----------

.. method:: sleep(ms)

.. include:: /includes/apiargs/method-sleep-param.rst

:method:`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 priod of time.

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

.. code-block:: javascript

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