Welcome to FindNerd. Today we are going to discuss mongoDB with nodejs. We use mongoDB with nodejs. MongoDB is nosql database. It is open source as well as cross-platform and well documented database. MongoDB uses collection which is set of mongoDB documents. In mongoDB each build-in database has its own files in application. A key-value pair is known as document with dynamic schema. Dynamic schema stands for documents in the same collection. In mongoDB we call table as collection, row as document, column as field, joins as embedded documents.
You can download the MongoDB from below mentioned link but you need to do some configuration to start the working.
https://www.mongodb.com/download-center?jmp=nav#community
You can also use the third party service that is mongolab or mlab where you can register free and select the sandbox plan for your development server. We have already registered in mlab and created the database named trains. Please check the screen-shot below.
![](http://findnerd.s3.amazonaws.com/imagedata/5697/5697.png)
You need to copy the connection string from mlab and paste inside your code. it will look like below.
mongodb://<dbuser>:<dbpassword>@ds023435.mlab.com:23435/trains
Replace <dbuser> with username and <dbpassword> with password of database.
You need to install the mongoose inside your application. Kindly use below command to install the module.
// to go your application directory
npm install
npm install --save mongoose
Above first command will install the all dependencies in your application and second command will install the mongoose module.
Below is the code for database connection. You can create a file database.js and put the code inside it.
var mongoose_mod = require('mongoose');
mongoose_mod.connect('mongodb://trains:trains@ds023435.mlab.com:23435/trains');
module.export = mongoose_mod.connection;
Last step is to load this file inside your main file. Kindly use below code for the same.
var db = require('./database');
Thank you for being with us!
0 Comment(s)