Databases and Collections数据库和集合

On this page本页内容

Overview概述

MongoDB stores data records as documents (specifically BSON documents) which are gathered together in collections.MongoDB将数据记录存储为文档(特别是BSON文档),这些文档以集合的形式聚集在一起。A database stores one or more collections of documents.数据库存储一个或多个文档的集合。

Databases数据库

In MongoDB, databases hold one or more collections of documents.在一个或多个数据库中保存一个或多个文档集合。To select a database to use, in the mongo shell, issue the use <db> statement, as in the following example:要选择要使用的数据库,请在mongo shell中发出use <db>语句,如下例所示:

use myDB

Create a Database创建数据库

If a database does not exist, MongoDB creates the database when you first store data for that database.如果数据库不存在,MongoDB会在您第一次存储该数据库的数据时创建该数据库。As such, you can switch to a non-existent database and perform the following operation in the mongo shell:因此,您可以切换到不存在的数据库,并在mongo shell中执行以下操作:

use myNewDB

db.myNewCollection1.insertOne( { x: 1 } )

The insertOne() operation creates both the database myNewDB and the collection myNewCollection1 if they do not already exist.如果数据库newinsertone()和集合mycollection1()都不存在,则insertOne()操作将创建这两者。Be sure that both the database and collection names follow MongoDB Naming Restrictions.确保数据库名称和集合名称都遵循MongoDB命名限制

Collections集合

MongoDB stores documents in collections.MongoDB将文档存储在集合中。Collections are analogous to tables in relational databases.集合类似于关系数据库中的表。

A collection of MongoDB documents.

Create a Collection创建集合

If a collection does not exist, MongoDB creates the collection when you first store data for that collection.如果集合不存在,MongoDB会在您第一次存储该集合的数据时创建该集合。

db.myNewCollection2.insertOne( { x: 1 } )
db.myNewCollection3.createIndex( { y: 1 } )

Both the insertOne() and the createIndex() operations create their respective collection if they do not already exist.insertOne()createIndex()操作都会创建各自的集合(如果它们还不存在)。Be sure that the collection name follows MongoDB Naming Restrictions.请确保集合名称遵循MongoDB命名限制

Explicit Creation显式创建

MongoDB provides the db.createCollection() method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules.MongoDB提供db.createCollection()法显式创建具有各种选项的集合,例如设置最大大小或文档验证规则。If you are not specifying these options, you do not need to explicitly create the collection since MongoDB creates new collections when you first store data for the collections.如果不指定这些选项,则不需要显式创建集合,因为MongoDB在您首次存储集合的数据时会创建新集合。

To modify these collection options, see collMod.若要修改这些集合选项,请参阅collMod

Document Validation文件验证

New in version 3.2.3.2版中新增

By default, a collection does not require its documents to have the same schema; i.e. the documents in a single collection do not need to have the same set of fields and the data type for a field can differ across documents within a collection.默认情况下,集合不要求其文档具有相同的架构;即单个集合中的文档不需要具有相同的字段集,并且字段的数据类型可以在集合中的不同文档中有所不同。

Starting in MongoDB 3.2, however, you can enforce document validation rules for a collection during update and insert operations.但是,从MongoDB 3.2开始,您可以在更新和插入操作期间对集合强制执行文档验证规则See Schema Validation for details.有关详细信息,请参见架构验证

Modifying Document Structure修改文档结构

To change the structure of the documents in a collection, such as add new fields, remove existing fields, or change the field values to a new type, update the documents to the new structure.要更改集合中文档的结构(例如添加新字段、删除现有字段或将字段值更改为新类型),请将文档更新为新结构。

Unique Identifiers唯一性标识符

New in version 3.6.3.6版中新增

Note

The featureCompatibilityVersion must be set to "3.6" or greater.featureCompatibilityVersion必须设置为3.6或更高。For more information, see View FeatureCompatibilityVersion.有关详细信息,请参见View FeatureCompatibilityVersion

Collections are assigned an immutable UUID.集合被分配了一个不可变的UUIDThe collection UUID remains the same across all members of a replica set and shards in a sharded cluster.集合UUID在复制集的所有成员和分片集群中的分片上保持相同。

To retrieve the UUID for a collection, run either the listCollections command or the db.getCollectionInfos() method.要检索集合的UUID,请运行listCollections命令或db.getCollectionInfos()方法。