|
| 1 | +.. _java-rs-run-command: |
| 2 | + |
| 3 | +====================== |
| 4 | +Run a Database Command |
| 5 | +====================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: database operation, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to run a database command with the |
| 24 | +{+driver-short+}. You can use database commands to perform a variety of administrative and |
| 25 | +diagnostic tasks, such as fetching server statistics, initializing a replica |
| 26 | +set, or running an aggregation pipeline. |
| 27 | + |
| 28 | +.. tip:: Prefer Driver Methods Over Database Commands |
| 29 | + |
| 30 | + The driver provides methods for many database commands. We recommend |
| 31 | + using driver methods instead of executing database commands when possible. |
| 32 | + |
| 33 | + To perform administrative tasks, use the :mdb-shell:`MongoDB Shell </>` instead of the |
| 34 | + {+driver-short+}. Calling the MongoDB Shell :manual:`db.runCommand() |
| 35 | + </reference/method/db.runCommand/>` method is the preferred method to issue |
| 36 | + database commands, as it provides a consistent interface between the shell |
| 37 | + and drivers. |
| 38 | + |
| 39 | +.. include:: includes/reactor-note.rst |
| 40 | + |
| 41 | +Run a Command |
| 42 | +------------- |
| 43 | + |
| 44 | +To run a database command, specify the command in a document, and pass the |
| 45 | +document to the ``runCommand()`` method. The following code calls the |
| 46 | +``runCommand()`` method on a database to run the ``ping`` command, which is a |
| 47 | +no-op command used to test if a server is responsive. |
| 48 | + |
| 49 | +.. io-code-block:: |
| 50 | + |
| 51 | + .. input:: /includes/write/run-command.java |
| 52 | + :language: java |
| 53 | + :emphasize-lines: 1, 3 |
| 54 | + |
| 55 | + .. output:: |
| 56 | + :visible: false |
| 57 | + |
| 58 | + Document{{ok=1}} |
| 59 | + |
| 60 | +Additional Information |
| 61 | +---------------------- |
| 62 | + |
| 63 | +To see a full list of database commands and their available parameters, see :manual:`Database |
| 64 | +Commands </reference/command>` in the {+mdb-server+} manual. |
| 65 | + |
| 66 | +API Documentation |
| 67 | +~~~~~~~~~~~~~~~~~ |
| 68 | + |
| 69 | +To learn more about the ``runCommand()`` method, see the |
| 70 | +`runCommand() API documentation <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#runCommand(org.bson.conversions.Bson)>`__. |
0 commit comments