MongoDB (六) MongoDB 集合操作
2015-08-30 14:36
591 查看
一. MongoDB 创建集合
createCollection() 方法
MongoDB db.createCollection(name, options) 是用来创建集合.语法:
基本的 createCollection() 命令语法如下:db.createCollection(name, options)
在命令中, name 是要创建的集合的名称. Options 是一个文件,用于指定配置的集合
参数 | 类型 | 描述 |
---|---|---|
Name | String | 要创建的集合名称 |
Options | Document | (可选)指定有关内存大小和索引选项 |
字段 | 类型 | 描述 |
---|---|---|
capped | Boolean | (可选)如果为true,则启用封顶集合。封顶集合是固定大小的集合,会自动覆盖最早的条目,当它达到其最大大小。如果指定true,则需要也指定尺寸参数。 |
autoIndexID | Boolean | (可选)如果为true,自动创建索引_id字段的默认值是false。 |
size | number | (可选)指定最大大小字节封顶集合。如果封顶如果是 true,那么你还需要指定这个字段。 |
max | number | (可选)指定封顶集合允许在文件的最大数量。 |
例子:
createCollection() 方法不使用选项的基本语法如下:>use test switched to db test >db.createCollection("mycollection") { "ok" : 1 } >
可以检查通过使用创建的集合命令 show collections
>show collections mycollection system.indexes
下面的例子显示了几个重要的选项 createCollection()方法的语法:
>db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ) { "ok" : 1 } >
在MongoDB中,不需要创建集合。当插入一些文件 MongoDB 自动创建的集合。
>db.yiibai.insert({"name" : "yiibai"}) >show collections mycol mycollection system.indexes yiibai >
二. MongoDB 删除集合
drop() 方法
MongoDB 的 db.collection.drop() 是用来从数据库中删除一个集合。语法:
drop() 命令的基本语法如下db.COLLECTION_NAME.drop()
示例:
首先,检查可用的集合在数据库 mydb>use mydb switched to db mydb >show collections mycol mycollection system.indexes yiibai >
现在删除集合名称为 mycollection
>db.mycollection.drop() true >
再次检查到数据库中的集合列表
>show collections mycol system.indexes yiibai >
drop() 方法将返回 true,如果选择成功收集被丢弃,否则将返回 false
相关文章推荐
- MongoDB (五) MongoDB 数据库操作
- MongoDB (四) MongoDB 数据模型
- MongoDB (三) MongoDB 安装
- MongoDB (二) MongoDB 优点
- MongoDB (一) MongoDB 介绍
- 8天学通MongoDB——第一天 基础入门
- mongodb常用操作命令
- MongoDB(2)-基本概念
- 请不要再用MongoDB了
- 用node.js+express.js+mongodb+jade开发一个完整的项目笔记(1)【页面即页面之间逻辑的制作】
- MongoDB的安全(五)
- mongodb的基本操作
- MongoDB(4)--MongoDB服务的启动
- MongoDB学习十--MongoDB的Replication Introduction
- mongodb笔记
- windows下安装mongoDB扩展
- MongoDB语法与现有关系型数据库SQL语法比较
- mongoDB 基本命令整理
- 在centOS中安装mongodb
- mongodb安装及简单操作