Example created Database in mongodb with nodejs use mongoose

Node.js can be used in database applications.

One of the most popular NoSQL database is MongoDB.

MongoDB

To be able experiment with the code examples, you will need access to a MongoDB database.

You can download a free MongoDB database at https://www.mongodb.com.

Install MongoDB Driver

Let us try to access a MongDB database with Node.js.

To download and install the official MongoDB driver, open the Command Terminal and execute the following:

Download and install mongoose package:

 

C:\ Nodejs>npm install mongoose

 

Now you have downloaded and installed a mongoose database driver.

Node.js can use this module to manipulate MongoDB databases:

 

var mongo = require('mongoose');

 

Creating a Database

To create a database in MongoDB, create a connection URL with the correct ip address and the name of the database you want to create.

MongoDB will create the database if it does not exist, and make a connection to it.

Example:

 

var mongoose = require('mongoose');

var db = mongoose.connection;

db.on('error', console.error);

db.once('open', function () {

   console.log("connect db codewr");

});

mongoose.connect('mongodb://192.168.1.122:27017/codewr');

 

Run:

 

C:\ Nodejs>node mongooseCreatedDatabase.js

Connect db codewr!