mongodb 创建集合
In MongoDB a collection is automatically created when it is referenced in any command. For example, if you run an insert command :
在MongoDB中,当在任何命令中引用集合时,都会自动创建一个集合。 例如,如果您运行插入命令:
db.student.insert({
name: "Viraj"
})
Above command will create a collection named student if it doesn't exist already in the database. But we can explicitly create a collection using the createCollection()
command. The syntax of createCollection method is:
如果数据库中尚不存在上述命令,则会创建一个名为student的集合。 但是我们可以使用createCollection()
命令显式创建一个集合。 createCollection方法的语法为:
db.createCollection(name, options)
In the above command, name will be the name of the collection and options is a document which can be used to specify configurations for the collection.
在上面的命令中, name将是集合的名称, options是可用于指定集合配置的文档。
options parameter is optional, and you can simply create a collection by just providing a name for the collection. But if you want to configure your collection, you can use various options available to do so. Following are the available configuration options for creating a collection in MongoDB:
options参数是可选的,您只需提供集合名称即可简单地创建集合。 但是,如果要配置集合,则可以使用各种可用选项进行配置。 以下是在MongoDB中创建集合的可用配置选项:
Field | Type | Description |
---|---|---|
capped | boolean | (Optional) To create a capped collection, where in we specify the the maximum size or document counts to prevent it from growing beyond a set maximum value. |
size | number | (Optional) To specify the maximum size in bytes for a capped collection. If a collection is capped and reaches its maximum size limit, MongoDB then removes older documents from it to make space for new. |
max | number | (Optional) This can be used to specify the maximum number of documents allowed in a capped collection. |
validator | document | (Optional) Validates any document inserted or updated against provided validation rules. |
validationLevel | string | (Optional) This is used to define how strictly validation rules must be applied to existing documents during an update. Available values are :
|
validationAction | string | (Optional) This can be used to set the action to be taken upon validation i.e. if any document fails the set validation then whether to show error or just warn about the violations. Default value is error . |
领域 | 类型 | 描述 |
---|---|---|
封顶 | 布尔值 | (可选)要创建一个有上限的集合,我们在其中指定最大大小或文档数,以防止其增长到超过设置的最大值。 |
尺寸 | 数 | (可选)指定上限集合的最大大小(以字节为单位)。 如果集合受到限制并达到最大大小限制,则MongoDB将从其中删除较旧的文档,以腾出空间容纳新文件。 |
最高 | 数 | (可选)可用于指定上限集合中允许的最大文档数。 |
验证器 | 文件 | (可选)根据提供的验证规则验证插入或更新的所有文档。 |
validationLevel | 串 | (可选)用于定义在更新期间必须如何严格地将验证规则应用于现有文档。 可用值为: |
validationAction | 串 | (可选)这可用于设置验证后要采取的操作,即,如果任何文档未通过设置的验证,则显示error 还是warn 违规。 默认值为error 。 |
MongoDB:创建一个封闭的集合 (MongoDB: Creating a Capped Collection)
We can create a capped collection using the following command.
我们可以使用以下命令创建一个有上限的集合。
db.createCollection("student", { capped : true, size : 5242880, max : 5000 } )
This will create a collection named student, with maximum size of 5 megabytes and maximum of 5000 documents.
这将创建一个名为Student的集合,最大大小为5 MB,最多5000个文档。
MongoDB:删除集合 (MongoDB: Drop a Collection)
Any collection in a database in MongoDB can be dropped easily using the following command:
使用以下命令可以轻松删除MongoDB数据库中的任何集合:
db.collection_name.drop()
drop()
method will return true is the collection is dropped successfully, else it will return false.
如果成功删除集合, drop()
方法将返回true,否则将返回false。
翻译自: https://www.studytonight.com/mongodb/collection-create-and-drop-mongodb
mongodb 创建集合