aneroid
(Anirudh Dutt)
7
I support the idea of having a way to prevent automatic collection creation.
But I think this issue occurs because of the convenience in the language API’s:
db.xyz.insert_one() in Python or db.xyz.insertOne() in JS will each treat “xyz” like it was a collection name; as long as it doesn’t conflict with an existing method on the db object.
- This dynamic attribute lookup means doing
db.get_collection("xyz").insert is less convenient to use/type, so the shortcut gets used and leads to these kinds of errors.
- I don’t think many experienced devs would use the shortcut-form and it’s a common pitfall/trap for anyone new to MongoDB, regardless of overall experience.
Instead of a per-command flag, I’d prefer a Server-side flag/env var either on the specific DB (preferred) or the whole instance. For example:
db.adminCommand({ setParameter: 1, autoCreateCollections: false })
- it would default to
true to remain compatible with all existing code
- DevOps, Leads, Sys Admins can explicitly set the flag on new and existing instances to prevent auto-creation in the future.
Main reason for not wanting a per-command flag: Anyone who “didn’t check the object type before inserting” or “didn’t use get_collection” will also not use or forget to use the require_existing_collection flag.
1 Like