Question

It happened several times that I made a typo in a collection name or forgot to switch database to that one which includes the desired collection. Mongo shell accepted silently all those commands. Is it possible that mongo shell will warn me if I try to execute a query on non-existing collection?

Was it helpful?

Solution

From the MongoDB console, there is no direct way to prevent the scenarios you mention.

You could add authentication/authorization to certain databases to help prevent some types of unintentional actions.

The intentional design of MongoDb is that it delay creates collections.

Unless you've scripted the console start, you'll end up in a test database.

If you're using the console, it is presumed that you have administrator control, and understand the risks of direct manipulation. A collection that doesn't exist is perfectly normal in many workflows.

I'd suggest you consider evaluating one of the many GUI applications for managing Mongo databases.

Many drivers have support to confirm that a collection exists before use, and can throw an error if the specified collection does not.

If the feature was added, it would need to be opt-in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top