start mongo db and write create collection in nodejs | nodejs tutorials

 write mongo db and conncec to server


var mongoose= require("mongoose");

mongoose.connect('mongodb://localhost:27017/mydatabase' ,
{
useNewUrlParser: true,
useUnifiedTopology: true
})
.then((res) => {
console.log(
'connected'
);
})
.catch((err) => {
console.log(
`not connected`,
err
);
});

ist start the mongo db server

then import the mongodb


connect to server

if it is connect it will print connect otherwise not connected


in below program 

connect to mongodb and if eror show this 

if open show this


var db = mongoose.connection;
db.on('error',console.error.bind(console,'connection error'));
db.on('open', function(){
console.log('connected');
});


No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...