6
在MongoDB中刪除集合的最佳方式是什麼?如何在MongoDB中刪除一個集合?
我使用以下:
db.collection.drop()
如the manual描述:
db.collection.drop()
從數據庫中刪除一個集合。該方法還會刪除與已刪除集合相關聯的任何 索引。該方法提供了圍繞放置命令的包裝器 。
但是我怎樣才能從命令行中刪除它?
在MongoDB中刪除集合的最佳方式是什麼?如何在MongoDB中刪除一個集合?
我使用以下:
db.collection.drop()
如the manual描述:
db.collection.drop()
從數據庫中刪除一個集合。該方法還會刪除與已刪除集合相關聯的任何 索引。該方法提供了圍繞放置命令的包裝器 。
但是我怎樣才能從命令行中刪除它?
因此,無論這些都是有效的方法來做到這一點:
mongo <dbname> --eval 'db.<collection>.drop()'
db.<collection>.drop()
這是我完全測試的方式,建立一個數據庫mytest
與集合hello
。
創建DB mytest
:
> use mytest
switched to db mytest
創建集合hello
:
> db.createCollection("hello")
{ "ok" : 1 }
顯示那裏所有的集合:
> db.getCollectionNames()
[ "hello", "system.indexes" ]
插入一些虛擬的數據:
> db.hello.insert({'a':'b'})
WriteResult({ "nInserted" : 1 })
確保它插入:
> db.hello.find()
{ "_id" : ObjectId("55849b22317df91febf39fa9"), "a" : "b" }
刪除集合,並確保它不存在任何更多:
> db.hello.drop()
true
> db.getCollectionNames()
[ "system.indexes" ]
這也適用(我不重複以前的命令,因爲它是強制性的t關於重新創建數據庫和集合):
$ mongo mytest --eval 'db.hello.drop()'
MongoDB shell version: 2.6.10
connecting to: mytest
true
$