5 MongoDB集合操作

2020-07-24  本文已影响0人  月影追猎者

创建集合

db.createCollection(name, options)
> use test
switched to db test
> db.createCollection("col")
{ "ok" : 1 }
> show collections
col

使用show collections或show tables命令查看已有集合

db.createCollection("col", { capped : true, size : 5120000, max : 10000 } )
{ "ok" : 1 }

创建固定集合,集合空间512000KB,文档最大数10000

当不创建集合插入文档时,MongoDB自动创建集合

> db.test.insert({ "key" : "value" })
WriteResult({ "nInserted" : 1 })
> show collections
test

删除集合

db.collection.drop()

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

> show collections
test
> db.test.drop()
true
> show collections
>
上一篇 下一篇

猜你喜欢

热点阅读