Skip to content

Commit

Permalink
DOCSP-39732 Run a command standardization (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-smith721 authored Aug 21, 2024
1 parent 32dcdcd commit 50f67a4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 71 deletions.
5 changes: 5 additions & 0 deletions source/includes/write/run-command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Document command = new Document("ping", 1);

Publisher<Document> commandPublisher = database.runCommand(command);
Document result = Mono.from(commandPublisher).block();
System.out.println(result);
2 changes: 1 addition & 1 deletion source/write-data-to-mongo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Write Data to MongoDB
/write/bulk-writes
/write/store-large-docs
/write/write-concern
/write/command
/write/run-command

Overview
--------
Expand Down
70 changes: 0 additions & 70 deletions source/write/command.txt

This file was deleted.

70 changes: 70 additions & 0 deletions source/write/run-command.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
.. _java-rs-run-command:

======================
Run a Database Command
======================

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: database operation, code example

Overview
--------

In this guide, you can learn how to run a database command with the
{+driver-short+}. You can use database commands to perform a variety of administrative and
diagnostic tasks, such as fetching server statistics, initializing a replica
set, or running an aggregation pipeline.

.. tip:: Prefer Driver Methods Over Database Commands

The driver provides methods for many database commands. We recommend
using driver methods instead of executing database commands when possible.

To perform administrative tasks, use the :mdb-shell:`MongoDB Shell </>` instead of the
{+driver-short+}. Calling the MongoDB Shell :manual:`db.runCommand()
</reference/method/db.runCommand/>` method is the preferred method to issue
database commands, as it provides a consistent interface between the shell
and drivers.

.. include:: includes/reactor-note.rst

Run a Command
-------------

To run a database command, specify the command in a document, and pass the
document to the ``runCommand()`` method. The following code calls the
``runCommand()`` method on a database to run the ``ping`` command, which is a
no-op command used to test if a server is responsive.

.. io-code-block::

.. input:: /includes/write/run-command.java
:language: java
:emphasize-lines: 1, 3

.. output::
:visible: false

Document{{ok=1}}

Additional Information
----------------------

To see a full list of database commands and their available parameters, see :manual:`Database
Commands </reference/command>` in the {+mdb-server+} manual.

API Documentation
~~~~~~~~~~~~~~~~~

To learn more about the ``runCommand()`` method, see the
`runCommand() API documentation <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoDatabase.html#runCommand(org.bson.conversions.Bson)>`__.

0 comments on commit 50f67a4

Please sign in to comment.