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. 在MongoDB中,数据库包含一个或多个文档集合。To select a database to use, in 要选择要使用的数据库,请在mongosh
, issue the use <db>
statement, as in the following example:mongosh
中发出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 因此,您可以切换到不存在的数据库,并在mongosh
:mongosh
中执行以下操作:
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. insertOne()
操作将创建数据库myNewDB
和集合myNewCollection1
(如果它们还不存在)。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.集合类似于关系数据库中的表。
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 MongoDB提供了db.createCollection()
method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules. 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文件验证
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唯一标识符
Collections are assigned an immutable UUID. 集合被分配了一个不可变的UUID。The 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 若要检索集合的UUID,请运行db.getCollectionInfos()
method.listCollections
命令或db.getCollectionInfos()
方法。