MongoDB 创建集合

1.手动创建:

语法格式:

db.createCollection(name, options)

参数说明:

  • name: 要创建的集合名称
  • options: 可选参数, 指定有关内存大小及索引的选项

options 可以是如下参数:

字段 类型 描述
capped 布尔 (可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。
当该值为 true 时,必须指定 size 参数。
autoIndexId 布尔 (可选)如为 true,自动在 _id 字段创建索引。默认为 false。
size 数值 (可选)为固定集合指定一个最大值(以字节计)。
如果 capped 为 true,也需要指定该字段。
max 数值 (可选)指定固定集合中包含文档的最大数

例如:在exam表中创建colle1集合:

> use exam
switched to db exam
> db.createCollection("colle1")
{ "ok" : }
> show collections #查看当前数据库的集合
colle1
system.indexes

创建固定集合 mycol,整个集合空间大小 6142800 KB, 文档最大个数为 10000 个。

> db.createCollection("mycol", { capped : true, autoIndexId : true, size :
, max : } )
{ "ok" : }
>

2.在 MongoDB 中,你不需要创建集合。当你插入一些文档时,MongoDB 会自动创建集合。

> show collections
colle1
system.indexes
> db.exam.insert({"name111" : "菜鸟教程"})
WriteResult({ "nInserted" : })
> show collections
colle1
exam
system.indexes

MongoDB 删除集合

语法格式:

db.collection.drop()

返回值

如果成功删除选定集合,则 drop() 方法返回 true,否则返回 false。

例如:查看所有的集合并删除其中一个集合

> show collections
colle1
exam
system.indexes
> show tables
colle1
exam
system.indexes
> db.colle1.drop()
true
> show tables
exam
system.indexes
>

  查看当前数据的集合可以用show collections或者show tables

最新文章

  1. sql的行转列(PIVOT)与列转行(UNPIVOT)
  2. Mysql 排名查询
  3. Adroid学习系列-入门(1)
  4. KMP算法心得
  5. 修改setup.py的源
  6. SQL 编辑
  7. ios英语口语800句应用源码
  8. [iOS UI进阶 - 3.0] 触摸事件的基本处理
  9. MySQL open table
  10. 编译android后找不到ramdisk-u.img[已解决]
  11. cf493B Vasya and Wrestling
  12. Kafka主要参数详解
  13. 【转】context和getApplicationContext()介绍
  14. QuartusII 中采用门级原语
  15. Java变量&&简单程序流程&&循环
  16. bzoj:1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
  17. [国嵌攻略][061][2440LCD驱动设计]
  18. VS2015创建ASP.NET应用程序描述
  19. platform驱动分离
  20. 根据传入的文件名称动态从moglifs图片服务器拿到pdf文档并在线浏览

热门文章

  1. 构建Http服务器
  2. jquery框架一点小心得
  3. Fiddler安卓抓包详细教程
  4. python 基础篇 12 装饰器进阶
  5. [P2387魔法森林
  6. LeetCode 29——两数相除
  7. PAT——乙级1015/甲级1062:德才论
  8. bbbbbeta
  9. Java面试题(上)
  10. Redis能做什么?不能做什么?