Skip to content

Commit b3a249d

Browse files
author
Mohammad Hunan Chughtai
authored
(DOCSP-8054): run a command usage example (#25)
* update verbiage * update TOC * update TOC * added command() method * added missing link faq + more description * Fixed note syntax * updated crud link * test push * (DOCSP-8054): Run a Command Usage Command init * added code snippet for db.command * updated verbiage * updated verbiage per pr comments * updated txt * updated txt
1 parent eb89c28 commit b3a249d

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ignored first line
2+
const { MongoClient } = require("mongodb");
3+
4+
// Replace the following with your MongoDB deployment's connection string.
5+
const uri =
6+
"mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
7+
8+
const client = new MongoClient(uri);
9+
10+
async function run() {
11+
try {
12+
await client.connect();
13+
14+
const db = client.db("sample_mflix");
15+
// find the storage statistics for the "sample_mflix" database using the 'dbStats' command
16+
const result = await db.command({
17+
dbStats: 1,
18+
});
19+
console.log(result);
20+
} finally {
21+
await client.close();
22+
}
23+
}
24+
run().catch(console.dir);

source/usage-examples.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Usage Examples
1515
:doc:`updateOne</usage-examples/updateOne>`
1616
:doc:`replaceOne</usage-examples/replaceOne>`
1717
:doc:`distinct</usage-examples/distinct>`
18+
:doc:`command</usage-examples/command>`
1819

1920
.. toctree::
2021
:caption: Examples
@@ -31,3 +32,4 @@ Usage Examples
3132
/usage-examples/updateOne
3233
/usage-examples/replaceOne
3334
/usage-examples/distinct
35+
/usage-examples/command

source/usage-examples/command.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=============
2+
Run A Command
3+
=============
4+
5+
.. default-domain:: mongodb
6+
7+
Overview
8+
--------
9+
10+
You can run all raw database operations, not including :manual:`CRUD </crud/>`
11+
operations, using the :node-api:`command() </Admin.html#~command>`
12+
method. Typically ``command()`` is used for diagnostic and
13+
administrative tasks, such as fetching server stats or initializing a
14+
replica set.
15+
16+
.. note::
17+
Use :manual:`collection methods </reference/method/js-collection>` instead of raw db commands whenever possible.
18+
19+
Create an `Object
20+
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object>`_
21+
to specify additional options. Set the ``maxTimeMS`` field of this object to state
22+
the number of milliseconds to wait before aborting the query.
23+
24+
The ``command()`` method returns a `Promise
25+
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise>`_
26+
that resolves to an object containing the return object of the operation that was run.
27+
28+
29+
.. literalinclude:: /code-snippets/usage-examples/command.js
30+
:language: javascript
31+
:linenos:
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
sasquatch?
2-
duplicate key error explanation
2+
duplicate key error explanation
3+
faq - diff b/w db.command({<crudMethod>}) and db.<crudMethod>

0 commit comments

Comments
 (0)