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

Hey Folks, It’s Worse Than We Thought — Claude’s Privacy Mess Just Got Bigger

  What started as a leak of shared Claude chats has turned into something much bigger — and much worse. The same thing is now happening with...