How to drop a collection in MongoDB? -


what best way drop collection in mongodb?

i using following:

db.collection.drop() 

as described in the manual:

db.collection.drop()

removes collection database. method removes indexes associated dropped collection. method provides wrapper around drop command.

but how can drop command line?

so either of these valid ways it:

mongo <dbname> --eval 'db.<collection>.drop()'  db.<collection>.drop() 

this way tested it, creating database mytest collection hello.

  • create db mytest:

    > use mytest switched db mytest 
  • create collection hello:

    > db.createcollection("hello") { "ok" : 1 } 
  • show collections there:

    > db.getcollectionnames() [ "hello", "system.indexes" ] 
  • insert dummy data:

    > db.hello.insert({'a':'b'}) writeresult({ "ninserted" : 1 }) 
  • make sure inserted:

    > db.hello.find() { "_id" : objectid("55849b22317df91febf39fa9"), "a" : "b" } 
  • delete collection , make sure not present more:

    > db.hello.drop() true > db.getcollectionnames() [ "system.indexes" ] 

this works (i not repeating previous commands, since recreating database , collection):

$ mongo mytest --eval 'db.hello.drop()' mongodb shell version: 2.6.10 connecting to: mytest true $ 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -