创建集合:createcollection() 方法
mongodb db.createcollection(name, options)
语法:
基本的 createcollection() 命令语法如下:
db.createcollection(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 mycollection system.indexes
>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 >
删除集合: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 >
如对本文有疑问, 点击进行留言回复!!
修复 Mac brew 安装 mongodb 报 Error: No available formula with the name ‘mongodb’ 问题详解
MongoDB中数据的替换方法实现类Replace()函数功能详解
网友评论