Why Mongoose? — Introduction to Mongoose
By: Divya Pawar
Tags: Mongoose, Nodejs, Mongodb

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It provides schema validation, used to translate between objects in code and the representation of those objects in MongoDB and manages relationships between data.

MongoDB stores data in the form of JSON document and document structure can vary as it is not enforced like SQL databases. Hence, it is known as schema-less NoSQL. document database. This is one of the advantages of using NoSQL as it speeds up application development and reduces the complexity of deployments.

Below is an example of how data is stored in Mongo vs. SQL Database:

MONGODB:

MYSQl:

Terminologies

Collections

‘Collections’ in Mongo are equivalent to tables in relational databases. They can hold multiple JSON documents.

Documents

‘Documents’ are equivalent to records or rows of data in SQL. While a SQL row can reference data in other tables, Mongo documents usually combine that in a document.

Fields

‘Fields’ or attributes are similar to columns in a SQL table.

Schema

While Mongo is schema-less, SQL defines a schema via the table definition. A Mongoose ‘schema’ is a document data structure (or shape of the document) that is enforced via the application layer.

Models

‘Models’ are higher-order constructors that take a schema and create an instance of a document equivalent to records in a relational database.

Installation:

Let’s suppose you already have setup node project with mongoDB. Installing mongoose is as easy as running the npm command.

npm install mongoose — save

Note: Make sure you have installed MongoDB for your OS or Have access to a MongoDB database.

Connecting to MongoDB database:

Import mongoose into the app:

import mongoose from 'mongoose';

2. Specify a Promise library:

mongoose.Promise = global.Promise;

3. Connect to MongoDB:

mongoose.connect(‘mongodb://127.0.0.1:27017/database’); /* Mongoose connection format looks something like this */ mongoose.connect(‘mongodb://USERNAME:PASSWORD@HOST::PORT/DATABASE_NAME’);

Note:

By default mongoose connects to MongoDB at port 27017, which is the default port used by MongoDB.

To connect to MongoDB hosted somewhere else, use the second syntax. Enter MongoDB username, password, host, and port and database name.

MongoDB port is 27017 by default; use your app name as the db name.

Connection with options and callback

Mongoose connect has 3 parameters, uri, options, and the callback function. To use them see sample below.

var mongoose = require(‘mongoose’); var uri = ‘mongodb://localhost:27017/DBNAME’; var options = { user: ‘user1’, pass: ‘pass’ } mongoose.connect(uri, options, function(err){ if (err) throw err; // if no error == connected });

Closing

It is a rich library full of useful and powerful features that make it easy to work with data models in the application layer.

While you can interact with Mongo directly using Mongo Driver, But Mongoose simplify interaction by allowing you to model relationships between data and validate them easily.

This article was just a basic introduction to Mongoose for those who wanted to get started with Mongoose. There is lot more to explore and know about Mongoose. We will look into it next time soon :)