26

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?

Community
  • 1
  • 1
static
  • 8,126
  • 15
  • 63
  • 89
  • 5
    do I see there an API `createNewDB(...)` ? no. How do I create a new DB using mongoose in node.js? – static May 09 '13 at 10:24

3 Answers3

21

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

Abhishek Kumar
  • 3,328
  • 2
  • 18
  • 31
  • 1
    So the is the URL specified only to parse the URL to mongodb server and the DB name? why don't do it separately? – static May 09 '13 at 22:01
  • This URL/mongoURI not only serves, specifying the server name but many other things. Check this out : http://docs.mongodb.org/manual/reference/connection-string/. Also you have to check your client-driver if they support all. Also, instead of passing the dbName to mongoURI, I am gussing you can do it separately. Atleast in java-driver, we can do it for sure. – Abhishek Kumar May 10 '13 at 00:09
0

I found a solution for me when trying to connect by mongoose.

!!!First of all you need to specify database even if you want to connect to default db (default db is 'admin') it's important!!! Before I specified /admin db my data was mixing from several db.

You need to specify credentials (as username and password) for correct database (in my case admin (I never created admin db before, it's default db)).

But what to do if you want to connect to another database? You should feel authSource method in options of connection (specify another database in uri: mongodb://localhost:27017/database):

mongoose.connect(
          mongoUri,
          {
            authSource: 'admin',
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useCreateIndex: true,
            user: config.mongo.username,
            pass: config.mongo.password,
            serverSelectionTimeoutMS: 5000,
          },
          (err) => {
            if (err != null) {
              error('mongoose', err.message);
            } else {
              info('mongoose', 'mongoose connected');
            }
          },
        )
-2

In Mongoose, a new database record is created when new mongoStore() is invoked:

  app.use(session({
    resave: false,
    saveUninitialized: true,
    secret: pkg.name,
    store: new mongoStore({
      url: config.db,
      collection : 'sessions'
    })
  }));

or

const mongoStore = require('connect-mongo')(session);
Jeff Voss
  • 3,637
  • 8
  • 46
  • 71
  • This answer appears to be specific to `express` and also does not answer the question very well. – Luke H May 04 '21 at 23:52