Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does mongoose.connect create a Mongo database by default if it doesn't exist?

In my Mongoose script I write:

mongoose.connect('mongodb://localhost/mydb');

But currently there is no mydb database in Mongo. Will this create mydbif it doesn't already exist?

like image 635
codeofnode Avatar asked Aug 30 '13 13:08

codeofnode


People also ask

Does Mongoose connect create a database?

Bookmark this question. Show activity on this post. databases are created on first write operation.

Does MongoDB create database if not exists?

Create a New Database : You can create a new Database in MongoDB by using “use Database_Name” command. The command creates a new database if it doesn't exist, otherwise, it will return the existing database. you can run this command in mongo shell to create a new database.

Does MongoDB automatically create database?

Unlike SQL, where we need to create a database, tables and then insert the values manually, MongoDB creates the database automatically. You just need to save a value in the defined collection with the preferred name. You don't even have to mention that you want to create a database.

Does Mongoose create collection automatically?

exports = mongoose. model('User',{ id: String, username: String, password: String, email: String, firstName: String, lastName: String }); It will automatically creates new "users" collection.


1 Answers

The command mongoose.connect('mongodb://localhost/mydb'); will indeed create the database mydb if it does not exist.

It will also create any collections you use in your app if they do not already exist in a similar fashion.

like image 61
mbaird Avatar answered Nov 01 '22 04:11

mbaird