Edupala

Comprehensive Full Stack Development Tutorial: Learn Ionic, Angular, React, React Native, and Node.js with JavaScript

Introducing Mongoose

Mongoose is a Node.js module that provides developers with the ability to model objects and save
them as MongoDB documents. While MongoDB is a schemaless database, Mongoose offers you the
opportunity to enjoy both strict and loose schema approaches when dealing with Mongoose models.

Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, predefine and custom validation, query building, business logic hooks and more, out of the box.

The basic features of Mongoose:

  • Mongoose schemas and models
  • Verifying your data using predefined and custom validators
  • Using middleware to intercept the model’s methods
  • Schema indexes, modifiers, and virtual attributes
  • Using the model’s methods and performing CRUD operations

Installing the mongoose: npm install –save mongoose, npm install

Contacting to MongoDB:
To connect to MongoDB, we will need to use the MongoDB connection URI.

mongodb://username:password@hostname:port/database

Since you’re connecting to a local instance, we can skip the username and password and use the
following URI:
mongodb://localhost/test

Defining URI in mongoose

mongoose.connect('mongodb://localhost/test');

Step 1: When a building a real application, saving the URI directly in the config/express.js
file is a bad practice. The best way store application variables are to use your environment
configuration file. Go to your config/env/development.js file and add the following code.

// Set the 'development' environment configuration object
module.exports = {
	db: 'mongodb://localhost/mean-development',
	sessionSecret: 'developmentSessionSecret'
};

In the real application, we need to define mongoose URI in config/env/production.js

Step 2: We required the Mongoose module and connected to the MongoDB instance using the db property of your configuration object. To In config folder, create a new file named config/mongoose.js , which contains the following code
snippet:

const config = require('./config');
const mongoose = require('mongoose');
module.exports = function() {
   const db = mongoose.connect(config.db);
   return db;
};

Add the following code in config/config.js

// Load the correct configuration file according to the 'NODE_ENV' variable
module.exports = require('./env/' + process.env.NODE_ENV + '.js');

Step 3: To initialize Mongoose configuration in server.js or app.js in root folder, add the following highlight code as

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const configureMongoose = require('./config/mongoose');
const configureExpress = require('./config/express');
const db = configureMongoose();
const app = configureExpress();

app.listen(3000);
module.exports = app;
console.log('Server running at http://localhost:3000/');
Introducing Mongoose

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top