Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Node.js with Mongoose

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 300
    Comment on it

    Mongoose is similar to ORM( Object relational mapper) in many other languages. It is a node.js package that provides mongoDB object mapping. So basically mongoose converts the data in the database to the javascript objects that can be used in our application.

    Using mongoose we can use mongoDB commands for CURD operation.

     

    Following are the steps to use mongoose in node application:

    Here I assume that you have Node.js and MongoDB installed. To use mongoose, we need to add it to our node application with the command below.

    npm install mongoose --save

     

    Now we will include this in our application and connect to a mongoDB database:

    var mongoose = require('mongoose');
        mongoose.connect('mongodb://localhost/myDatabase');
        var db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error:'));
        db.once('open', function callback () {
          
          console.log("Connected to database");
    
        });

     

    Schemas and Models:
    Now we are connected to the database, but in order to perform CRUD operation, we need to have a schema and a model.
    Schema is used to define the attributes or we can say the structure of a document. Also, we can define methods on mongoose schema.

    Models are used to create instances of the data, that is going to be saved in documents.
    Let's understand this with example so create a directory model and a file user.js:

    var mongoose = require('mongoose');
    var schema = mongoose.Schema;
    //create a schema
    var userSchema = new schema({
    username: { type: String, required: true},
    email: { type: String, required: true},
    password: { type: String, required: true},
    age : Number,
    created_at : Date,
    updated_at : Date
    });
    // create a model
    
    var userModel = mongoose.model('User', userSchema);
    module.exports = userModel;

     

    Now we have a User model so we can call in our code so first we include the above file:

    var User = require('./app/models/user');
    // create a new user  
    var newUser = new User({
      username: 'name',
      email: 'email',
      password: 'password'
    });
    
    newUser.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
    });
    
    // get all the users
    User.find({}, function(err, result) {
      if (err) throw err;
    
      // object of all the users
      console.log(result);
    });

    So that is how we can use mongoose in our node application and use mongoDB methods to perform CURD operations.

 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: