Så något av dessa är giltiga sätt att göra det:
mongo <dbname> --eval 'db.<collection>.drop()'
# ^^^^^^^^ ^^^^^^^^^^^^
db.<collection>.drop()
# ^^^^^^^^^^^^
Till exempel för en samling mycollection i en databas mydb du skulle säga:
mongo mydb --eval 'db.mycollection.drop()'
db.mycollection.drop()
Så här testade jag det fullständigt och skapade en databas mydb med en samling hello .
-
Skapa db
mydb:> use mydb switched to db mydb -
Skapa en samling
mycollection:> db.createCollection("mycollection") { "ok" : 1 } -
Visa alla samlingar där:
> db.getCollectionNames() [ "mycollection", "system.indexes" ] -
Infoga lite dummy-data:
> db.mycollection.insert({'a':'b'}) WriteResult({ "nInserted" : 1 }) -
Se till att den satts i:
> db.mycollection.find() { "_id" : ObjectId("55849b22317df91febf39fa9"), "a" : "b" } -
Ta bort samlingen och se till att den inte finns längre:
> db.mycollection.drop() true > db.getCollectionNames() [ "system.indexes" ]
Detta fungerar också (jag upprepar inte de tidigare kommandona, eftersom det bara handlar om att återskapa databasen och samlingen):
$ mongo mydb --eval 'db.mycollection.drop()'
MongoDB shell version: 2.6.10
connecting to: mydb
true
$