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, 您可以从Atlas UI、mongosh, or MongoDB Compass. 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 本页介绍如何从Atlas UI管理Atlas集群上的数据库和集合。对于自我管理的部署,您可以使用mongosh or MongoDB Compass to manage databases and collections.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, MongoDB Shell,mongosh, is a JavaScript and Node.js REPL environment for interacting with MongoDB deployments. 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并执行以下操作:
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:要选择要使用的数据库,请完成以下步骤:
Start MongoDB Compass and connect to your cluster.启动MongoDB Compass并连接到您的集群。
To learn more, see Connect to MongoDB.要了解更多信息,请参阅连接到MongoDB。
Create a Database创建数据库
Atlas UI
To create a new database, log in to Atlas and do the following:要创建新数据库,请登录Atlas并执行以下操作:
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 如果数据库不存在,MongoDB会在您首次存储该数据库的数据时创建数据库。因此,您可以切换到一个不存在的数据库,并在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. Be sure that both the database and collection names follow MongoDB Naming Restrictions.insertOne()操作创建数据库myNewDB和集合myNewCollection1(如果它们不存在)。确保数据库和集合名称都遵循MongoDB命名限制。
Collections集合
MongoDB stores documents in collections. Collections are analogous to tables in relational databases.MongoDB将文档存储在集合中。集合类似于关系数据库中的表。
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并执行以下操作:
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命名限制。
Explicit Creation显式创建
Atlas UI
To create a new collection, log in to Atlas and do the following:要创建新集合,请登录Atlas并执行以下操作:
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:您可以创建以下类型的集合之一:
-
If you select to create a capped collection, specify the maximum size in bytes.如果选择创建上限集合,请指定最大大小(以字节为单位)。 -
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.如果选择创建聚集集合,则必须指定聚集索引键值和聚集索引的名称。
mongosh
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。
MongoDB Compass
Click the name of the database where you to want to create a collection in the left navigation.在左侧导航中单击要在其中创建集合的数据库的名称。
Enter the name of the collection and optionally, configure additional preferences.输入集合的名称,并可选择配置其他首选项。
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 要检索集合的UUID,请运行db.getCollectionInfos() method. listCollections命令或db.getCollectionInfos()方法。