源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解MongoDB中创建集合与删除集合的操作方法

  • 时间:2021-07-20 23:14 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解MongoDB中创建集合与删除集合的操作方法
[b]创建集合:createCollection() 方法 [/b]
MongoDB db.createCollection(name, options)
 是用来创建集合.
语法: 基本的 createCollection() 命令语法如下:
db.createCollection(name, options)

在命令中, name 是要创建的集合的名称. Options 是一个文件,用于指定配置的集合
[center]参数[/center] [center]类型[/center] [center]描述[/center]
Name String 要创建的集合名称
Options Document (可选)指定有关内存大小和索引选项
选项​​参数是可选的,所以只需要到指定的集合名称。以下是可以使用的选项列表:
[center]字段[/center] [center]类型[/center] [center]描述[/center]
capped Boolean (可选)如果为true,则启用封顶集合。封顶集合是固定大小的集合,会自动覆盖最早的条目,当它达到其最大大小。如果指定true,则需要也指定尺寸参数。
autoIndexID Boolean (可选)如果为true,自动创建索引_id字段的默认值是false。
size number (可选)指定最大大小字节封顶集合。如果封顶如果是 true,那么你还需要指定这个字段。
max number (可选)指定封顶集合允许在文件的最大数量。
当插入文档,MongoDB 第一检查大小字段封顶集合,然后它会检查最大的字段中。
例子:  createCollection() 方法不使用选项的基本语法如下:
 >use test switched to db test >db.createCollection("mycollection") { "ok" : 1 } >

可以检查通过使用创建的集合命令 show collections
 >show collections mycollection system.indexes

下面的例子显示了几个重要的选项 createCollection()方法的语法:
[code] >db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ) { "ok" : 1 } > [/code]在MongoDB中,不需要创建集合。当插入一些文件 MongoDB 自动创建的集合。  [code]>db.yiibai.insert({"name" : "yiibai"}) >show collections mycol mycollection system.indexes yiibai >[/code] [b]删除集合:drop() 方法[/b] 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
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部