Search code examples
node.jsmongodbmongoose

Mongoose.connect doesn't create database if not exist


I'm new on NodeJs, I'm learning how to connect to mongodb using NodeJS with Mongoose library. As I know, when I connect to database with a name, if this database with that name doesn't exist, then mongoose will create it automatically, but it's not being created with me. Here is my code:

const mongoose = require("mongoose");

mongoose.connect("mongodb://127.0.0.1:27017/mongo-test", {useNewUrlParser: true})
.then(() => console.log("Connected"))
.catch(err => console.log(err));

mongoose version: ^5.2.5


Solution

  • I'm not 100% sure but you also need to create a record so that it creates the database. Only specifying the database name on the connection string isn't enough apparently.

    Cheers