Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop mongodb creating dbs and collections dynamically

Tags:

mongodb

Is there a way to switch off the ability of mongo to sporadically create dbs and collections as soon as it sees one in a query. I run queries on the mongo console all the time and mistype a db or collection name, causing mongo to just create one. There should be a switch to have mongo only explicitly create dbs and collections. I can't find one on the docs.

like image 280
Martin Klosi Avatar asked May 24 '14 00:05

Martin Klosi


Video Answer


1 Answers

To be clear, MongoDB does not auto create collections or databases on queries. For collections, they are auto created when you actually save data to them. You can test this yourself, run a query on a previously unknown collection in a database like this:

use unknowndb
db.unknowncollection.find()
show collections

No collection named "unknowncollection" shows up until you insert or save into it.

Databases are a bit more complex. A simple "use unknowndb" will not auto create the database. However, if after you do that you run something like "show collections" it will create the empty database.

I agree, an option to control this behavior would be great. Happy to vote for it if you open a Jira ticket at mongoDB.

like image 111
John Petrone Avatar answered Oct 25 '22 05:10

John Petrone