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. 如果数据库myNewDB和集合myNewCollection1不存在,则insertOne()操作会创建它们。Be sure that both the database and collection names follow MongoDB Naming Restrictions.确保数据库和集合名称都遵循MongoDB命名限制

Collections集合

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

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文档校验

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 db.getCollectionInfos() method.要检索集合的UUID,请运行listCollections命令或db.getCollectionInfos()方法。

←  Getting StartedViews →