File tree Expand file tree Collapse file tree 4 files changed +59
-1
lines changed
code-snippets/usage-examples Expand file tree Collapse file tree 4 files changed +59
-1
lines changed Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ Usage Examples
15
15
:doc:`updateOne</usage-examples/updateOne>`
16
16
:doc:`replaceOne</usage-examples/replaceOne>`
17
17
:doc:`distinct</usage-examples/distinct>`
18
+ :doc:`command</usage-examples/command>`
18
19
19
20
.. toctree::
20
21
:caption: Examples
@@ -31,3 +32,4 @@ Usage Examples
31
32
/usage-examples/updateOne
32
33
/usage-examples/replaceOne
33
34
/usage-examples/distinct
35
+ /usage-examples/command
Original file line number Diff line number Diff line change
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:
Original file line number Diff line number Diff line change 1
1
sasquatch?
2
- duplicate key error explanation
2
+ duplicate key error explanation
3
+ faq - diff b/w db.command({<crudMethod>}) and db.<crudMethod>
You can’t perform that action at this time.
0 commit comments