Docs Menu
Docs Home
/ / /
Java Reactive Streams Driver
/

Run a Database Command

On this page

  • Overview
  • Run a Command
  • Additional Information
  • API Documentation

In this guide, you can learn how to run a database command with the Java Reactive Streams driver. 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 MongoDB Shell instead of the Java Reactive Streams driver. Calling the MongoDB Shell db.runCommand() method is the preferred method to issue database commands, as it provides a consistent interface between the shell and drivers.

Important

Project Reactor Library

This guide uses the Project Reactor library to consume Publisher instances returned by the Java Reactive Streams driver methods. To learn more about the Project Reactor library and how to use it, see Getting Started in the Reactor documentation. To learn more about how we use Project Reactor library methods in this guide, see the Write Data to MongoDB guide.

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.

Document command = new Document("ping", 1);
Publisher<Document> commandPublisher = database.runCommand(command);
Document result = Mono.from(commandPublisher).block();
System.out.println(result);
Document{{ok=1}}

To see a full list of database commands and their available parameters, see Database Commands in the MongoDB Server manual.

To learn more about the runCommand() method, see the runCommand() API documentation.

Back

Write Concern