数据库和集合
MongoDB 以分层结构组织数据。一个 MongoDB 部署包含一个或多个数据库,每个数据库包含一个或多个集合。在每个集合中,MongoDB 将数据存储为包含字段和值对的文档。
先决条件
您必须将以下导入语句包含在程序中,才能运行本指南中的代码示例
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
实现,这在 Subscriber
中有所描述快速入门指南.
连接到 MongoDB 部署
首先,连接到一个正在运行的 MongoDB 部署。
以下代码连接到在本地主机 localhost
的端口 27017
上运行的独立 MongoDB 部署
MongoClient mongoClient = MongoClients.create();
要了解有关连接到 MongoDB 部署的更多信息,请参阅连接到 MongoDB 教程。
访问数据库
一旦你有一个连接到 MongoDB 部署的 MongoClient
实例,请使用 `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 兆字节的定长集合
database.createCollection( "cappedCollection", new CreateCollectionOptions().capped(true).sizeInBytes(0x100000) ).subscribe(new OperationSubscriber<Void>());
有关定长集合的更多信息,请参阅服务器手册中的 定长集合。
文档验证
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>());
有关文档验证的更多信息,请参阅服务器手册中的 Schema Validation。
获取集合列表
您可以使用 MongoDatabase.listCollectionNames()
方法获取数据库中集合的列表
database.listCollectionNames().subscribe(new PrintToStringSubscriber<String>());
删除集合
您可以使用 MongoCollection.drop()
方法删除集合并删除集合中的所有数据
MongoCollection<Document> collection = database.getCollection("contacts"); collection.drop().subscribe(new OperationSubscriber<Void>());
不可变性
MongoDatabase
和 MongoCollection
实例是不可变的。为了从具有不同属性(如不同的 读取关注点、读取首选项 和 写入关注点)的现有实例创建新实例,MongoDatabase
和 MongoCollection
类提供了以下方法:
MongoDatabase.withReadConcern()
MongoDatabase.withReadPreference()
MongoDatabase.withWriteConcern()
MongoCollection.withReadConcern()
MongoCollection.withReadPreference()
MongoCollection.withWriteConcern()
CodecRegistry
getCollection()
方法的一个重载允许您指定用于表示 BSON 文档的不同类。例如,您可能希望使用严格且类型安全的 BsonDocument
类来在执行 CRUD 操作时建模您的文档。
// 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
实例必须在MongoCollection
的CodecRegistry
中注册。Codec
实例必须能够编码和解码完整的 BSON 文档,而不仅仅是例如Int32
这样的单个 BSON 值。
默认情况下,MongoCollection
配置了三个类的 Codec
实例:
Document
BsonDocument
BasicDBObject
应用程序可以通过自定义 CodecRegistry
来注册其他类的 Codec
实现。新的 CodecRegistry
实例在以下级别可配置:
在
MongoClient
中的MongoClientSettings
内在
MongoDatabase
的withCodecRegistry
方法内在
MongoCollection
的withCodecRegistry
方法内
考虑编码和解码 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);