您的位置:首页 > 数据库 > Mongodb

MongoDB 创建集合

2016-07-25 10:20 555 查看

createCollection() 方法

MongoDB db.createCollection(name, options) 是用来创建集合.

语法:

基本的 createCollection() 命令语法如下:

db.createCollection(name, options)


在命令中, name 是要创建的集合的名称. Options 是一个文件,用于指定配置的集合

参数类型描述
NameString要创建的集合名称
选项参数是可选的,所以只需要到指定的集合名称。以下是可以使用的选项列表:

字段类型描述
cappedBoolean(可选)如果为true,则启用封顶集合。封顶集合是固定大小的集合,会自动覆盖最早的条目,当它达到其最大大小。如果指定true,则需要也指定尺寸参数。
autoIndexIDBoolean(可选)如果为true,自动创建索引_id字段的默认值是false。
sizenumber(可选)指定最大大小字节封顶集合。如果封顶如果是 true,那么你还需要指定这个字段。
maxnumber(可选)指定封顶集合允许在文件的最大数量。
当插入文档,MongoDB 第一检查大小字段封顶集合,然后它会检查最大的字段中。

例子:

createCollection() 方法不使用选项的基本语法如下:

> use test
switched to db test
> db.createCollection("ithome")
{ "ok" : 1 }
>


可以检查通过使用创建的集合命令 show collections

> show collections
ithome
person


下面的例子显示了几个重要的选项 createCollection()方法的语法:

> db.createCollection("person", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }
>


在MongoDB中,不需要创建集合。当插入一些文件 MongoDB 自动创建的集合。

> db.article.insert({"title" : "hellomonogo"})
> show collections ithome person
article
>


作者:itmyhome
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: