hoge
hoge

Reputation: 65

Can't remove collection in mongoDB

I just started learning mongoDB and I noticed a collection which is removed by .remove() command still exist after the executing. am I doing something wrong or this is how it's supposed to work?

by using mongo

use testDB
db.stats() // returns "db" : "testDB","collections" : 0,"objects" : 0 
//and db.getCollectionNames() returns nothing as well
db.testCollection.insert({ test : 'abc'})
db.getCollectionNames() // [ "system.indexes", "testCollection" ]
db.testCollection.remove()
db.testCollection.find() // returns nothing
db.getCollectionNames() // [ "system.indexes", "testCollection" ]
db.stats() // "db" : "testDB","collections" : 3,"objects" : 4

Upvotes: 1

Views: 1947

Answers (1)

Salvador Dali
Salvador Dali

Reputation: 222531

You missed the point of remove operation in mongodb. It does not remove the collection, it removes all documents in the collection which specify the query. If you do remove() you specify nothing in your query, thus it removes everything.

To remove collection do db.collection.drop()

Upvotes: 4

Related Questions