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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -