In my Mongoose script I write:
mongoose.connect('mongodb://localhost/mydb');
But currently there is no mydb
database in Mongo. Will this create mydb
if it doesn't already exist?
Bookmark this question. Show activity on this post. databases are created on first write operation.
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.
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.
exports = mongoose. model('User',{ id: String, username: String, password: String, email: String, firstName: String, lastName: String }); It will automatically creates new "users" collection.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With