• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Setup Node.js with MongoDB on Ubuntu & Debian

Written by Rahul, Updated on July 5, 2018

This article will help you to Install MongoDB with NodeJs and configure their connectivity using Mongoose node app on Ubuntu and Debian systems. For PHP users use our previous article to configure MongoDB with PHP and Apache2 on Ubuntu servers.

Prerequsities

We assume you already have Node.js and MongoDB installed on your system. If not installed follow our below tutorial first to complete the required installation.

MongoDB Installation:

  • Install MongoDB on Ubuntu
  • Install MongoDB on Debian

Node.js Installation:

  • Install Node.js on Ubuntu
  • Install Node.js on Debian

Install “mongoose” Module

Mongoose provides a straight-forward, schema-based solution to modeling your application data and includes built-in typecasting, validation and many more.

sudo npm install mongoose

Connect Node.js to MongoDB

Create a test.js file and add following content to the file. For the more details about working with Nodejs and MongoDB with mongoose read this tutorial.

//This code requires mongoose node module
var mongoose = require('mongoose');

//connecting local mongodb database named test
var db = mongoose.connect('mongodb://127.0.0.1:27017/test');

//testing connectivity
mongoose.connection.once('connected', function() {
	console.log("Database connected successfully")
});

Now lets execute the test.js using node. If you get message “Database connected successfully”, It means your node.js app is successfully connecting database.

node test.js

Database connected successfully

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy