0

Hi I am new to node and mongo DB development. I created one simple node application. I am using mongoDB. I have initialise mondo db and able to insert some data. In my app.js I added DB connection like as follow:

const mongoose = require('mongoose');
const mongoDB = 'mongodb://localhost:27017';
mongoose.connect(mongoDB);
mongoose.Promise = global.Promise;
const db = mongoose.connection;

My schema class looks like

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let UserSchema = new Schema({
    // publisherId: {type: String, required: true},
    userName: {type: String, required: true},
    age: {type: Number, required: true}
},{timestamps: true });


// Export the model
module.exports = mongoose.model('User', UserSchema);

now I want to access same data from command line so I tried to connect my database.

mongo mongodb://localhost:27017 

It connect with this db without any error. When I try to access the data it does not show any data.I check which all databases are available. it shows default test database.

1 Answer 1

1

The database is missing from your connection string.

Try something like:

const connect = async () => {
  await mongoose.connect('mongodb://localhost/my-database')
}

I guess mongoose is using default as a default when the connection string does not specify any database to use.

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

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.