Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new db in mongoose?

Tags:

If I use Mongoose in node.js I need to do:

var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/myDB'); 

But what if I just installed mongodb and I don't have any DB at the moment? How can I create a new DB using mongoose in node.js before doing mongoose.connect('mongodb://localhost/myDB')? Or if there is no such myDB then it will create a new one? or will throw an error that there is no such a DB?

like image 935
static Avatar asked May 09 '13 10:05

static


People also ask

Can Mongoose create database?

Yes, you can specify the database name in your connection string. and that will create all your records under the name 'dbname2'.

Can we create database in MongoDB?

Creating a MongoDB Database with the CLIMongoDB only creates the database when you first store data in that database. This data could be a collection or even a document. To add a document to your database, use the db.

How do I create a database in Studio 3T?

In Studio 3T:In the Connection Tree, right-click on the target server. Choose Add Database. Name your database – and note the info message. Click on OK.


1 Answers

It should not throw any error. The moment you insert a document in any new collection of that DB, the collection and db will be created

like image 122
Abhishek Kumar Avatar answered Oct 18 '22 16:10

Abhishek Kumar