5

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?

Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
  • I think it is not possible unless you use a driver – anvarik Mar 17 '14 at 10:25
  • Agreeing with @anvarik there is no possibility in the shell to detect that you have "miss-typed" something. A shell is just a "prototype" utility anyway and should be treated as such. Various drivers allow "safe" operations to make sure your statements do not end up in creating "new" collections. – Neil Lunn Mar 17 '14 at 10:43

1 Answers1

1

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.

WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
  • I agree that for example insert operation should create a collection if it doesn't exist. It's relatively easy to notice that there's something wrong with a find query if it returns no results. However, when I tried to perform an update I realized just after several attempts that the reason of no changes was wrong collection name. BTW @WiredPrairie which Mongo GUI do you recommend? I use MonjaDB but when I want to update multiple documents at once I have to wire command which is not validated against collection existence. – Lukasz Wiktor Mar 17 '14 at 11:30
  • RE: GUIs. It depends what OS platform you're on really and what you're comfortable with. http://docs.mongodb.org/ecosystem/tools/administration-interfaces/ – WiredPrairie Mar 17 '14 at 12:20
  • Unfortunately, the `update` you tried was also technically correct by design with MongoDB. You might want to try adding it to their feature/bug database for MongoDB here: https://jira.mongodb.org/secure/Dashboard.jspa. – WiredPrairie Mar 17 '14 at 12:22
  • I've just created a feature request in their issue tracker: https://jira.mongodb.org/browse/SERVER-13225 – Lukasz Wiktor Mar 17 '14 at 13:48
  • 1
    @LukaszWiktor have a look to RoboMongo, nice UI with a console support – anvarik Mar 18 '14 at 08:06