20

I am trying to figure out if I can create a new database in MongoDB with Mongoose. I am running on Node, and I know the MongoDB driver for Node can do it, but I am wondering if I can do it just from Mongoose.

Is there an equivalent to the db.createCollection(name, options) from the Node MongoDB driver in Mongoose? My google skills are failing right now.

I just was trying to figure out if I had to install the whole MongoDB driver just for that, but I think I do.

2
  • 6
    What is your usecase for creating a new collection with mongoose? that's not something you would normally need to do with mongoose, other than the fact that mongoose will create a collection for a schema if it doesn't already exist. Commented Jun 29, 2015 at 22:08
  • I think I might have mixed up the terminology; I just want to create a database if it doesn't exist without having to install the whole driver just for that. I'm editing the question. Commented Jun 30, 2015 at 8:46

3 Answers 3

43

Yes, you can specify the database name in your connection string.

db = mongoose.connect('mongodb://localhost/dbname1')

As soon as you create a record with that connection, it will create the new database and collections under the database name of 'dbname1'. If you wanted to create a new database, you can specify a different connection string:

db = mongoose.connect('mongodb://localhost/dbname2')

and that will create all your records under the name 'dbname2'. Your documents will not import over to dbname2, you will have to do an import of those records if you wanted to do that. Hope that helps.

Sign up to request clarification or add additional context in comments.

1 Comment

This should still work, the Mongoose documentation is here: mongoosejs.com/docs/connections.html mongoose.connect('mongodb://localhost/myapp'); You might be running into a different error
3

If you want to create database for online cluster you also need to enter your database name when you pass the uri like:-

"mongodb+srv://<Username>:<Password>@resumeapp.3ditras.mongodb.net/<DatabaseName>?retryWrites=true&w=majority"

Comments

-3

Just Try this code it's very simple and clean

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/yourdatabasename').then(() => console.log('Connected to MongoDB...')).catch((err) => console.error("Coudn't connect MongoDB....", err));

const customerSchema= new mongoose.Schema({ name: String,address: String,email:String,});

const Customer= mongoose.model('Customer',courseSchema);

async function createNewCustomer() {const customer= new Customer({name: 'new customer',address: 'new address',email: '[email protected]',});const result = await customer.save();console.log(result);
}
createNewCustomer();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.