Database Manual / Introduction

Databases and Collections in MongoDBMongoDB中的数据库和集合

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.数据库存储一个或多个文档集合。

You can manage databases and collections on the Atlas cluster from the Atlas UI, mongosh, or MongoDB Compass. 您可以从Atlas UI、mongosh或MongoDB Compass管理Atlas集群上的数据库集合This page describes how to manage databases and collections on the Atlas cluster from the Atlas UI. For self-managed deployments, you can use mongosh or MongoDB Compass to manage databases and collections.本页介绍如何从Atlas UI管理Atlas集群上的数据库和集合。对于自我管理的部署,您可以使用mongosh或MongoDB Compass来管理数据库和集合。

Select the client that you want to use to manage databases and collections.选择要用于管理数据库和集合的客户端。

Atlas UI

MongoDB Atlas is a multi-cloud database service that simplifies deploying and managing your databases on the cloud providers of your choice.MongoDB Atlas是一种多云数据库服务,可简化在您选择的云提供商上部署和管理数据库。

mongosh

The MongoDB Shell, mongosh, is a JavaScript and Node.js REPL environment for interacting with MongoDB deployments. MongoDB Shell,mongosh,是一个用于与MongoDB部署交互的JavaScript和Node.js REPL环境。To learn more, see mongosh.要了解更多信息,请参阅mongosh

MongoDB Compass

MongoDB Compass is a powerful GUI for querying, aggregating, and analyzing your MongoDB data in a visual environment. To learn more, see MongoDB Compass.MongoDB Compass是一个强大的GUI,用于在可视化环境中查询、聚合和分析MongoDB数据。要了解更多信息,请参阅MongoDB Compass

Databases数据库

In MongoDB, databases hold one or more collections of documents.在MongoDB中,数据库保存一个或多个文档集合。

Atlas UI

To select a database to use, log in to Atlas and do the following:要选择要使用的数据库,请登录Atlas并执行以下操作:

1

Navigate to the Collections tab.导航到“集合”选项卡。

2

Select the database from the list of databases in the left pane.从左侧窗格中的数据库列表中选择数据库。

mongosh

To select a database to use, in mongosh, issue the use <db> statement, as in the following example:要选择要使用的数据库,请在mongosh中发出use <db>语句,如下例所示:

use myDB
MongoDB Compass

To select a database to use, complete the following steps:要选择要使用的数据库,请完成以下步骤:

1

Start MongoDB Compass and connect to your cluster.启动MongoDB Compass并连接到您的集群。

To learn more, see Connect to MongoDB.要了解更多信息,请参阅连接到MongoDB

2

Select Databases from the left navigation.从左侧导航中选择数据库。

The Databases tab opens to list the existing databases for your MongoDB deployment.“数据库”选项卡打开,列出MongoDB部署的现有数据库。

Create a Database创建数据库

Atlas UI

To create a new database, log in to Atlas and do the following:要创建新数据库,请登录Atlas并执行以下操作:

1

Navigate to the Collections tab.导航到“集合”选项卡。

2

Click Create Database.单击“创建数据库”。

3

Enter the Database Name and the Collection Name.输入“数据库名称”和“集合名称”。

Enter the database and the collection name to create the database and its first collection.输入数据库和集合名称以创建数据库及其第一个集合。

4

Click Create.单击“创建”。

Upon successful creation, the database and the collection displays in the left pane in the Atlas UI.成功创建后,数据库和集合将显示在Atlas UI的左侧窗格中。

mongosh

If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform the following operation in mongosh:如果数据库不存在,MongoDB会在您首次存储该数据库的数据时创建数据库。因此,您可以切换到一个不存在的数据库,并在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. Be sure that both the database and collection names follow MongoDB Naming Restrictions.insertOne()操作创建数据库myNewDB和集合myNewCollection1(如果它们不存在)。确保数据库和集合名称都遵循MongoDB命名限制

MongoDB Compass
1

Open the Databases tab.打开“数据库”选项卡。

2

Click the Create database button.单击“创建数据库”按钮。

3

Enter database and first collection names in the Create Database dialog.在“创建数据库”对话框中输入数据库和第一个集合名称。

4

Click Create Database to create the database and its first collection.单击“创建数据库”以创建数据库及其第一个集合。

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会在您首次存储该集合的数据时创建该集合。

Atlas UI

To create a new collection, log in to Atlas and do the following:要创建新集合,请登录Atlas并执行以下操作:

1

Navigate to the Collections tab.导航到“集合”选项卡。

2

Click the + icon for the database.单击数据库的+图标。

3

Enter the name of the collection.输入集合的名称。

4

Click Create.单击“创建”。

Upon successful creation, the collection displays underneath the database in the Atlas UI.成功创建后,该集合将显示在Atlas UI中的数据库下方。

mongosh
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命名限制

MongoDB Compass
1

Click the name of the database where you to want to create a collection in the left navigation.在左侧导航中单击要在其中创建集合的数据库的名称。

2

Click the + icon next to the database name.单击数据库名称旁边的+图标。

3

Enter the name of the collection in the Create Collection dialog.在“创建集合”对话框中输入集合的名称。

4

Click Create Collection to create the collection.单击“创建集合”以创建集合。

Explicit Creation显式创建

Atlas UI

To create a new collection, log in to Atlas and do the following:要创建新集合,请登录Atlas并执行以下操作:

1

Navigate to the Collections tab.导航到“集合”选项卡。

2

Click the + icon for the database.单击数据库的+图标。

3

Enter the name of the collection.输入集合的名称。

4

Optional. From the Additional Preferences dropdown, select the type of collection that you want to create.可选。从“附加首选项”下拉列表中,选择要创建的集合类型。

You can create one of the following types of collections:您可以创建以下类型的集合之一:

  • Capped collection封顶集合

    If you select to create a capped collection, specify the maximum size in bytes.如果选择创建上限集合,请指定最大大小(以字节为单位)。

  • Time Series Collection时间序列集合

    If you select to create a time series collection, specify the time field and granularity. You can optionally specify the meta field and the time for old data in the collection to expire.如果选择创建时间序列集合,请指定时间字段和粒度。您可以选择指定元字段和集合中旧数据的过期时间。

  • Clustered Index Collection聚集索引集合

    If you select to create a clustered collection, you must specify clustered index key value and a name for the clustered index.如果选择创建聚集集合,则必须指定聚集索引键值和聚集索引的名称。

5

Click Create.单击“创建”。

Upon successful creation, the collection displays underneath the database in the Atlas UI.成功创建后,该集合将显示在Atlas UI中的数据库下方。

mongosh

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

MongoDB Compass
1

Click the name of the database where you to want to create a collection in the left navigation.在左侧导航中单击要在其中创建集合的数据库的名称。

2

Click the Create collection button.单击“创建集合”按钮。

3

Enter the name of the collection and optionally, configure additional preferences.输入集合的名称,并可选择配置其他首选项。

4

Click Create Collection to create the collection.单击“创建集合”以创建集合。

MongoDB Compass provides the following additional preferences that you can configure for your collection:MongoDB Compass提供了以下可以为您的集合配置的附加首选项:

Schema 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.默认情况下,集合不要求其文档具有相同的模式;即,单个集合中的文档不需要具有相同的字段集,并且字段的数据类型可以在集合中的不同文档之间有所不同。

However, you can enforce schema validation rules for a collection during update and insert operations. See Schema Validation for details.但是,您可以在更新和插入操作期间对集合强制实施架构验证规则。有关详细信息,请参阅架构验证

For deployments hosted in MongoDB Atlas, the Performance Advisor and the MongoDB Atlas UI detect common schema design issues and suggest modifications that follow MongoDB best practices. 对于在MongoDB Atlas中托管的部署,Performance Advisor和MongoDB Atlas UI会检测常见的模式设计问题,并建议遵循MongoDB最佳实践进行修改。To learn more, see Schema Suggestions.要了解更多信息,请参阅架构建议

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. The collection UUID remains the same across all members of a replica set and shards in a sharded cluster.集合被分配了一个不可变的UUID。集合UUID在副本集的所有成员和分片集群中的分片之间保持不变。

Atlas UI
mongosh

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

MongoDB Compass