文档菜单
文档首页
/ / /
Java 反应式流驱动程序
/

运行命令

在本页面上

  • 先决条件
  • 连接到 MongoDB 部署
  • 运行 buildInfo 命令

并非所有数据库命令都有特定的辅助方法。但是,您可以使用MongoDatabase.runCommand() 方法运行任何 MongoDB 命令。

有关 MongoDB 命令的更多信息,请参阅服务器手册中的数据库命令

为了运行本指南中的代码示例,您必须设置以下组件

  • 一个包含从文档资产GitHub中的文档的test.restaurants集合

  • 以下导入语句

import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoDatabase;
import org.bson.Document;

重要

本指南使用Subscriber实现,这些实现在本指南的快速入门指南.

首先,连接到MongoDB部署,然后声明并定义一个MongoDatabase实例。

以下代码连接到在本地主机localhost上运行端口号为27017的独立MongoDB部署。然后,它定义database变量以引用test数据库

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");

要了解更多关于连接到MongoDB部署的信息,请参阅连接到MongoDB教程。

要运行 buildInfo 命令,构造一个指定命令的 Document 对象,并将其作为参数传递给 runCommand() 方法。

以下示例代码运行 buildInfo 命令并打印结果

database.runCommand(new Document("buildInfo", 1)).subscribe(new PrintDocumentSubscriber());

返回

GridFS