stop mongodb creating dbs and collections dynamically

前端 未结 3 1790
南方客
南方客 2021-01-11 10:58

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 ti

相关标签:
3条回答
  • 2021-01-11 11:18

    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.

    0 讨论(0)
  • 2021-01-11 11:20

    For people who are using Mongoose, a new database will get created automatically if your Mongoose Schema contains any form of index. This is because Mongo needs to create a database before it can insert said index.

    0 讨论(0)
  • 2021-01-11 11:38

    No, implicit creation of collections and DBs is a feature of the console and may not be disabled. You might take a look at the security/authorization/role features of 2.6 and see if anything might help (although there's not something that exactly matches your request as far as I know).

    I'd suggest looking through the MongoDB issues/bug/requests database system here to and optionally add the feature request if it doesn't already exist.

    0 讨论(0)
提交回复
热议问题