Book Image

Web Development with MongoDB and NodeJS Second edition

Book Image

Web Development with MongoDB and NodeJS Second edition

Overview of this book

Table of Contents (19 chapters)
Web Development with MongoDB and NodeJS Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
12
Popular Node.js Web Frameworks
Index

A short introduction to MongoDB


As we discussed in the previous paragraph, MongoDB falls into the document store category of NoSQL databases. MongoDB is being actively developed by 10gen, which was later renamed to MongoDB I.inc. MongoDB is open source and its source is available on various platforms such as GitHub.

Features of MongoDB

One of the most important reasons for the popularity of MongoDB is that it is a JSON-friendly database. It means that documents are stored and retrieved from MongoDB as JavaScript objects. Internally, this JSON data gets converted to BSON format while getting persisted to the system. So, this gives an extreme flexibility where we can use the same data format from client to server and eventually to the database.

A typical document (record) in a MongoDB collection (table) might look like the following code:

$ mongo
> db.contacts.find({email: '[email protected]'}).pretty()
{
   "email" : "[email protected]",
   "phone" : "123-456-7890",
   "gravatar" : "751e957d48e31841ff15d8fa0f1b0acf",
   "_id" : ObjectId("52fad824392f58ac2452c992"),
   "name" : {
      "first" : "Jason",
      "last" : "Krol"
   },
   "__v" : 0
}

Another important feature of MongoDB is its schemaless nature. With a relational database, you are required to define (ahead of time) the exact structure of the data being stored, which is termed as the schema. This means that you must have defined the exact number of columns, length, and data type for every field in a table, and that each field must always match that exact set of criteria. Mongo provides a flexible nature where the documents that you store into the database need not follow any schema unless the developer enforces it through the application level. This makes MongoDB a great fit for Agile-based development, as you could carry out modifications on the application schema on fly.

Other than the JavaScript-friendly nature, one other resemblance of MongoDB with Node.js is that it is also designed with highly concurrent applications with heavy read operations in mind.

MongoDB also introduces the concept of sharding, which makes it possible to scale the database horizontally as well as vertically. If the application owner needs to increase the database capabilities, they could add up more machines into the stack. This is a cheaper option compared to investing on RAM of a single machine, which will be the case in RDBMS solutions.

All the advantages that we discussed come with some impact on the consistency, as MongoDB does not strictly adhere to the RDBMS standards like ACID transactions. Also, if you end up creating a data model that might need too many JOIN operations, then MongoDB won't make a good fit as it is not designed with too many aggregations even though the aggregations are possible via the MongoDB aggregation framework. MongoDB may or may not be the right solution for your application. You should truly weigh the pros and cons of each technology before making a decision to determine which technology is right for you.