//Import the mongoose modulevar mongoose =require('mongoose');//Set up default mongoose connectionvar mongoDB ='mongodb://127.0.0.1/my_database';
mongoose.connect(mongoDB,{useNewUrlParser:true,useUnifiedTopology:true});//Get the default connectionvar db = mongoose.connection;//Bind connection to error event (to get notification of connection errors)
db.on('error',console.error.bind(console,'MongoDB connection error:'));
-In your entry file
mongoose
.connect("mongodb://localhost/vidly").then(()=>console.log("Connected to MongoDB...")).catch((err)=>console.log("Cloud not connect to MongoDB..."));
// local conecationconst mongoose =require("mongoose");
mongoose
.connect("mongodb://localhost:27017/testdb").then(()=>console.log("Connected to MongoDB...")).catch((err)=>console.error("Could not connect to MongoDB...", err));
//connect with mongodb
mongoose.connect('mongodb://localhost:27017/your_db_name',{useNewUrlParser:true});//you can also specify with user and pass
mongoose.connect('mongodb://username:password@host:port/database?options...',{useNewUrlParser:true});//or goto docs https://mongoosejs.com/docs/connections.html
// getting-started.jsconst mongoose =require('mongoose');main().catch(err=>console.log(err));asyncfunctionmain(){await mongoose.connect('mongodb://localhost:27017/test');// use `await mongoose.connect('mongodb://user:password@localhost:27017/test');` if your database has auth enabled}