Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Chapter 2: How to connect with mongoDb and use mongoDb model using nodeJS

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 569
    Comment on it

    NodeJS is an open source, completely free and javascript based platform built on Google Chrome's JavaScript V8 Engine. In other words, we can say that NodeJS is a cross platform which is used to develop server side web application.

    In my previous chapter, I have explained that how we will install and create a project using nodeJs and expressJs. Now I will explain how to connect our project with mongoDb and use mongoDb model to save data in mongoDb.

    So step by step connectivity with mongoDb and use mongoDb model using nodeJS are as follow:

     

     

    Step 1:First we will go to app.js and write below code on app.js.

    var mongoose = require('mongoose');
    //define database
    var configDB = require('./config/database.js');
    
    mongoose.connect(configDB.url);

     

     

    Step 2: Now we will create one folder named as config and then create a file database.php in that folder. There we will mention our database in database.php.

    module.exports = {
               'url' : 'mongodb://localhost/databaseName' 
    };

     

    Step 3: Now we will create a form where data will come. I am not creating a form, you have to create your own.

     

    Step 4: When you click on a form you will define a URL on your routes.php.

    app.post('/saveUserData', userController.saveUserData);

     

    The above line tells that when u click on form submit button it will come to the routes.php and call the User Controller and call the function saveUserData. Then we will create UserController.js. There we write the following code.

     

    var userSchema=require('../app/models/user.js');
    
    module.exports.saveUserData = function(request, response) {
             var userData=new userSchema({
                             username:request.body.userName,
                             age:request.body.age,
                             email:request.body.email,
             });
             userData.save(function(err,saveUserData){
                        if(err)
                               response.json(err);
                       else
                               console.log('Data is saved'+saveUserData);
             });  
             
    };

     

    In the above code, we will make an object of a user model and then save data on according to user schema. Now we will create User schema.

     

    Step 5: Now create user schema in model folder.

    var mongoose = require('mongoose');
    var bcrypt   = require('bcrypt-nodejs');
    
    var userSchema = mongoose.Schema({
            username     :String,
            email            : String,
            age               : Number,
    });
    module.exports = mongoose.model('User', userSchema);

     

    By this way, we can connect our node.js application with mongoDb and insert data on mongoDb.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: