//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:'));
// 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));
// 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}