Brandon Clark
Brandon Clark

Reputation: 796

Collection name in Mongoose

Why would a database named 'blog' not allow a record insert and also give no return error and why would a database named 'blogs' allow a record inserts and return errors?


I just spent several hours going through all my code thinking I did something wrong. I have written many mongoose connected apps but when using the following it would return success but not insert the record and return no error as to why:

mongooose.connect('mongodb://localhost:27017/blog');

After banging my head against a wall for a bit I decided to change the database name:

mongooose.connect('mongodb://localhost:27017/blogs');

It works! But why would this name convention matter? I can't find anything in the documentation for MongoDB or Mongoosejs.

Upvotes: 1

Views: 213

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 145994

So I'm fairly certain mongodb doesn't care about database name "blog" vs "blogs". However, do note that mongoose has the questionably-helpful feature of silently queueing up operations while the database connection is still not established and then firing them off if/when the database connection is ready. That could be causing your confusion. To test that theory, pass a callback to mongoose.connect and put a console.log in the callback so you know exactly when the connection is ready.

Upvotes: 1

Related Questions