0

I create my new Project clientside i am using Angular6 and Serverside i use Node.js now i want to make database connectivity node.js with MongoDB. Previously i used Mysql database, then i heard about MongoDB database but i dont know how to connect MongoDB database with Node.js kindly help me how to connect node.js with MongoDB(which packages i have to install all that and then how to connect,for that good websites and links are available kinly share)

in mysql i used phpmyadmin is used, for mongoDB what is used ?

app.js(node.js)

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
   next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

3 Answers 3

3

If you want to deploy your app, mlab its a good option

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/<mydb>";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");
});

A good option for handle the DB is mongoose (https://www.npmjs.com/package/mongoose)

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

3 Comments

for mysql i used phpmyadmin now for MongoDB what i have to use ?
If you want to use a graphic interface, you can use mongo compass, Robo 3T, you can use the shell as well, I recommend you to see the official documentation to check out what is the best tool for you MongoDB Documentation
Now dont delete I make mistake I got answer from you Thank you so much
0

Mongoose is a npm module that is widely used for connecting to MongoDB. You can use it with local db or mlab.com.

https://mongoosejs.com/

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

Comments

0
mongoose = require("mongoose");

const DB = () => {
    mongoose.connect("mongodb://localhost:27017/crud", (err) => {
        if (!err) {
            console.log('connection established');
        } else {
            console.log(' Not connection');
        }
    });
}
module.exports = DB;

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.