Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Nodejs»How to Setup Node.js with MongoDB on Ubuntu & Debian

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

    RahulBy RahulMay 22, 20141 Min ReadUpdated: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
    
    mongodb Mongoose NodeJs nodejs mongo connect
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Setup MongoDB, PHP5 & Apache2 on Ubuntu
    Next Article Sending Emails using PHP with PHPMailer

    Related Posts

    How To Install NVM on Ubuntu 22.04

    Updated:April 16, 20223 Mins Read

    How To Install NVM on Windows

    Updated:April 16, 20223 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    How To Install Node.Js on Debian 11

    Updated:February 11, 20226 Mins Read

    How To Install and Secure MongoDB on Ubuntu 20.04

    Updated:June 18, 20224 Mins Read

    How To Install NVM on Debian 11

    Updated:August 31, 20213 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    • How to Install Angular CLI on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.