mongodb(三)-集合

参考:

http://www.yiibai.com/mongodb/mongodb_create_collection.html

一、创建集合

    语法:db.createCollection(name,options)

               name:String类型,要创建集合的名称

               options:Document类型,可选,制定有关内存大小和索引选项

               选项​​参数是可选的,所以只需要到指定的集合名称。以下是可以使用的选项列表:

字段 类型 描述
capped Boolean (可选)如果为true,则启用封顶集合。封顶集合是固定大小的集合,会自动覆盖最早的条目,当它达到其最大大小。如果指定true,则需要也指定尺寸参数。
autoIndexID Boolean (可选)如果为true,自动创建索引_id字段的默认值是false。
size number (可选)指定最大大小字节封顶集合。如果封顶如果是 true,那么你还需要指定这个字段。
max number (可选)指定封顶集合允许在文件的最大数量。
 
> use joan
switched to db joan
> db.createCollection('dora')
{ "ok" : 1 }
> use joan
switched to db joan
> db.createCollection('dora')
{ "ok" : 0, "errmsg" : "collection already exists", "code" : 48 }

    查看创建的集合:

> show collections
dora
system.indexes

    有可选参数的创建集合:

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

   插入文件时,mongodb会自动创建集合:

> db.joan2.insert({"name" : "dora"})
WriteResult({ "nInserted" : 1 })
> show collections
dora
joan1
joan2
system.indexes

 二、删除集合

扫描二维码关注公众号,回复: 479322 查看本文章

    语法:db.COLLECTION_NAME.drop()

> use joan
switched to db joan
> show collections
dora
joan1
joan2
system.indexes
> db.joan2.drop()
true
> show collections
dora
joan1
system.indexes

    

                                          

猜你喜欢

转载自joandora.iteye.com/blog/2230775