mongodb 创建集合_MongoDB:创建集合

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中创建集合的可用配置选项:

FieldTypeDescription
cappedboolean(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.
sizenumber(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.
maxnumber(Optional) This can be used to specify the maximum number of documents allowed in a capped collection.
validatordocument(Optional) Validates any document inserted or updated against provided validation rules.
validationLevelstring(Optional) This is used to define how strictly validation rules must be applied to existing documents during an update.

Available values are :

off No validation for inserts or updates.

strict This is the default value for this option. This instructs MongoDB to apply validation rules to all inserts and updates.

moderate This is used when we want to apply validation rules to inserts and updates on only the existing valid documents and not on the existing invalid documents.

validationActionstring(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 (可选)用于定义在更新期间必须如何严格地将验证规则应用于现有文档。

可用值为:

off不验证插入或更新。

strict这是此选项的默认值。 这指示MongoDB将验证规则应用于所有插入和更新。

moderate这是当我们要验证规则适用于插入和更新仅在现有的有效单据,而不是在现有的无效文档中使用。

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 创建集合

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值