On this page本页内容
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.数据库存储一个或多个文档的集合。
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>
语句,如下例所示:
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中执行以下操作:
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命名限制。
MongoDB stores documents in collections.MongoDB将文档存储在集合中。Collections are analogous to tables in relational databases.集合类似于关系数据库中的表。
If a collection does not exist, MongoDB creates the collection when you first store data for that collection.如果集合不存在,MongoDB会在您第一次存储该集合的数据时创建该集合。
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 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
。
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.有关详细信息,请参见架构验证。
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.要更改集合中文档的结构(例如添加新字段、删除现有字段或将字段值更改为新类型),请将文档更新为新结构。
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.集合被分配了一个不可变的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()
方法。