Docs 菜单
Docs 主页
/ / /
Java Reactive Streams 驱动程序
/

数据库和集合

在此页面上

  • 先决条件
  • 连接到 MongoDB 部署
  • 访问数据库
  • 访问集合
  • 创建集合
  • 获取集合列表
  • 删除集合
  • 不变性
  • CodecRegistry

MongoDB 以分层结构组织数据。 MongoDB 部署包含一个或多个数据库,每个数据库又包含一个或多个集合。 在每个集合中,MongoDB 将数据存储为包含字段与值对的文档。

您必须在程序中包含以下 import 语句才能运行本指南中的代码示例:

import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoCollection;
import com.mongodb.reactivestreams.client.MongoDatabase;
import static com.mongodb.client.model.Filters.*;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.client.model.ValidationOptions;

重要

本指南使用 Subscriber实现,如快速入门入门知识中所述。

首先,连接到正在运行的 MongoDB 部署。

以下代码连接到在端口27017上的localhost上运行的独立 MongoDB 部署:

MongoClient mongoClient = MongoClients.create();

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

MongoClient实例连接到 MongoDB 部署后,使用`getDatabase()方法访问数据库。

将数据库名称作为参数传递给getDatabase()方法。 如果数据库不存在,MongoDB 会在您向数据库插入任何数据时创建该数据库。

以下示例将访问test数据库:

MongoDatabase database = mongoClient.getDatabase("test");

注意

MongoDatabase 实例不可变。 要了解详情,请参阅本指南的不变性部分。

创建MongoDatabase实例后,使用getCollection()方法从该数据库访问集合。

将集合名称作为参数传递给getCollection()方法。

使用在上一节中创建的database实例,以下代码访问名为myTestCollection的集合:

MongoCollection<Document> coll = database.getCollection("myTestCollection");

注意

MongoCollection 实例不可变。 要了解更多信息,请参阅本指南的“不变性”部分。

如果具有该名称的集合不存在,MongoDB 会在您首次将数据插入该集合时创建该集合。

您还可以使用各种选项直接创建集合,例如设置最大大小或创建文档验证规则。

驱动程序提供了createCollection()方法来直接创建集合。 创建集合时,可以使用CreateCollectionOptions类指定各种集合选项,例如最大大小或文档验证规则。

如果未指定任何选项,则无需直接创建集合,因为 MongoDB 会在您首次插入数据时自动创建新集合。

以下操作将创建一个限制为1 MB 的固定大小集合:

database.createCollection(
"cappedCollection",
new CreateCollectionOptions().capped(true).sizeInBytes(0x100000)
).subscribe(new OperationSubscriber<Void>());

要了解有关固定大小集合的更多信息,请参阅 MongoDB Server手册中的 固定大小集合 。

MongoDB 允许您在更新和插入期间验证文档。 验证规则是使用ValidationOptions类在集合级别指定的,该类采用指定验证规则或表达式的筛选器文档。

以下示例将创建一个具有模式验证功能的集合:

ValidationOptions collOptions = new ValidationOptions().validator(
Filters.or(Filters.exists("email"), Filters.exists("phone")));
database.createCollection(
"contacts",
new CreateCollectionOptions().validationOptions(collOptions)
).subscribe(new OperationSubscriber<Void>());

要了解有关文档验证的更多信息,请参阅 MongoDB Server手册中的 模式验证 。

您可以使用MongoDatabase.listCollectionNames()方法获取数据库中集合的列表:

database.listCollectionNames().subscribe(new PrintToStringSubscriber<String>());

您可以使用MongoCollection.drop()方法删除一个集合并删除该集合中的所有数据:

MongoCollection<Document> collection = database.getCollection("contacts");
collection.drop().subscribe(new OperationSubscriber<Void>());

MongoDatabaseMongoCollection实例是不可变的。 要从具有不同属性(例如不同的读关注读取偏好写关注)的现有实例创建新实例, MongoDatabaseMongoCollection类提供了以下方法:

  • MongoDatabase.withReadConcern()

  • MongoDatabase.withReadPreference()

  • MongoDatabase.withWriteConcern()

  • MongoCollection.withReadConcern()

  • MongoCollection.withReadPreference()

  • MongoCollection.withWriteConcern()

要了解更多信息,请参阅读取操作写入操作教程。

getCollection()方法的重载允许您指定不同的类来表示 BSON 文档。 例如,在执行 CRUD 操作时,您可能希望使用严格且类型安全的BsonDocument类来对文档进行建模:

// pass BsonDocument.class as the second argument
MongoCollection<BsonDocument> collection = database
.getCollection("mycoll", BsonDocument.class);
// insert a document
BsonDocument document = BsonDocument.parse("{x: 1}");
collection.insertOne(document).subscribe(new OperationSubscriber<Void>());
document.append("x", new BsonInt32(2)).append("y", new BsonInt32(3));
// replace a document
collection.replaceOne(Filters.eq("_id", document.get("_id")), document)
.subscribe(new PrintSubscriber<UpdateResult>("Update Result: %s"));
// find documents
collection.find().subscribe(new PrintDocumentSubscriber());

任何类都必须满足两个要求才能以此方式使用:

  • Codec 该类的实例必须在MongoCollectionCodecRegistry中注册。

  • Codec 实例必须是对完整 BSON 文档进行编码和解码的实例,而不仅仅是像Int32这样的单个 BSON 值。

默认情况下, MongoCollection配置有以下三个类的Codec实例:

  • Document

  • BsonDocument

  • BasicDBObject

应用程序可通过自定义CodecRegistry来自由注册其他类的Codec实现。 新的CodecRegistry实例可在以下级别进行配置:

  • MongoClientSettings内的MongoClient

  • MongoDatabasewithCodecRegistry方法中

  • MongoCollectionwithCodecRegistry方法中

考虑对UUID类的实例进行编码和解码的情况。 默认情况下,驱动程序使用与其他 MongoDB 驱动程序不兼容的字节顺序对UUID的实例进行编码,更改默认值会很危险。

需要跨多个驱动程序互操作性的新应用程序可以更改该默认值,并且可以通过指定 CodecRegistry

// replaces the default UuidCodec to use the standard UUID representation
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)
), MongoClientSettings.getDefaultCodecRegistry());
// globally
MongoClientSettings settings = MongoClientSettings.builder()
.codecRegistry(codecRegistry).build();
MongoClient client = MongoClients.create(settings);
// or per database
MongoDatabase database = client.getDatabase("mydb")
.withCodecRegistry(codecRegistry);
// or per collection
MongoCollection<Document> collection = database.getCollection("mycoll")
.withCodecRegistry(codecRegistry);

后退

压缩

来年

创建索引