Chapter 8. Managing Data Using MongoDB

MongoDB is the database for the MEAN stack, and we have already explored some of its more basic features. It is an extremely powerful, scalable, NoSQL database that has gained wide popularity for big data and web applications. It happens to be open source and supported on a wide variety of operating systems and platforms.

MongoDB can be accessed using the MongoDB shell, a command-line interface that uses JavaScript-like syntax.

In this chapter, we will explore MongoDB in greater depth and begin to incorporate it into our SPA. You will explore the various CRUD operations in the MongoDB shell, as well as using a Node.js plugin to access a database inside your single page application.

We will cover the following topics:

  • NoSQL databases
  • Commanding MongoDB using the shell
  • Incorporating MongoDB into the SPA
  • MongoDB performance

Exploring the NoSQL database model

MongoDB is one of a number of NoSQL databases. Currently, it happens to be the most popular NoSQL database in use, according to statistics gathered by those who watch databases. SQL-based, relational databases have served us well for decades, so what's the big deal with NoSQL?

Defining NoSQL

MongoDB is often referred to as a NoSQL database. NoSQL is a popular buzzword that applies to MongoDB and several other database engines. But what does it mean?

First, there is no standard definition by some governing body defining what NoSQL means. The term was first used in 1998 by Carlo Strozzi to describe an open source relational database that did not have an SQL interface. However, today the term is used differently. NoSQL databases tend to have two defining features.

NoSQL

As the name might imply, most NoSQL databases do not use SQL to access the database. There are some NoSQL databases, however, that allow languages that are SQL-like or derived from SQL. Therefore, some take NoSQL to mean not only SQL.

MongoDB databases are normally accessed through JavaScript-like syntax.

Non-relational

NoSQL databases do not use the relational model, where data is stored in structured tables of columns or rows. In the case of MongoDB, data is stored as documents in collections.

In relational databases, data is stored in tables, much like a table in a spreadsheet.

Distributed

MongoDB, and other NoSQL databases, are designed to be distributed to work well in clusters. This makes hosting NoSQL databases in the cloud among numerous servers easier and provides for security, backup, performance, and scaling.

MongoDB supports sharding. Sharding is a process where portions of the database are hosted on different servers. This can make MongoDB extremely fast and highly scalable.

While it's beyond the scope of this book, the distributed nature of MongoDB makes it appealing for big data projects. Certainly, it makes MongoDB a compelling solution for web applications, which is its most popular use currently.

Features of MongoDB

Mongo has a number of features you should be aware of that make it different from other databases. They are explained as follows.

Document model

There are a number of models used by NoSQL databases. Some of these include the graph model, key-value model, object model, and others. These other models are beyond the scope of this book.

MongoDB uses the document model. Data is stored in collections of documents in a MongoDB database.

Here's an example of a MongoDB document:

{  
"_id" : ObjectId("566d9d4c1c09d090fd36ba82"), 
"name" : "John",  
"address" : {  
"street" : "77 Main street",  
"city" : "Springfield" }  
} 

As you can see, documents in MongoDB are a form of JSON. In this case, the document even contains a subdocument, the address.

The database itself binary encodes the documents and stores them in a form referred to as BSON. Not to worry, though, you will not have to be concerned about encoding or decoding any of the data yourself, that is all handled behind the scenes.

One of the main differences between JSON and BSON is that BSON supports a number of data types not supported by JSON. This includes binary data, regular expressions, symbols, dates, and so on. For example, a date may be represented in JSON output as a simple string. However, storing a date as date type in BSON allows efficient date comparisons and operations as part of queries or insertions.

For the most part, this is not something you'll need to worry about. MongoDB will seamlessly convert the data into usable JSON. However, when we get to Mongoose, data validation will be an important feature that will be handled by middleware.

Schemaless

One of the features of MongoDB, and some other NoSQL databases, is that it doesn't have a fixed schema.

In MongoDB, documents are stored in groups called collections. Documents stored in a collection should be related conceptually, but there is no restriction in the database software itself that enforces this. This is in stark contrast to databases where schemas strictly define the data which can be entered into a table.

There is a danger here that random documents can be placed into any collection making the organization of the collections meaningless. You could insert a document reflecting data for a car into a collection called pets, but this wouldn't make much sense and could render the data in that collection difficult to query meaningfully.

It bears some thought.

Open source

MongoDB is an open source database. A number of various licenses apply to the server itself, the drivers, tools, and the documentation.

Complete licensing information for MongoDB is available at https://www.mongodb.org/licensing.

Why use MongoDB?

There are many choices of databases you could use to build a single page web application. For example, MySQL is a popular database for web applications overall. Why would you want to choose MongoDB over something like MySQL?

Ultimately, almost any database will do the job, but there are certain features in MongoDB that make it particularly attractive for use in SPAs.

Well supported

MongoDB enjoys wide support on a number of operating systems and platforms. MongoDB has downloads and installers for Windows, multiple flavors of Linux, Mac, and Solaris.

One of the popular ways to run MongoDB in the cloud is on a Platform as a Service (PaaS). PaaS is a service, normally provided by a vendor such as Amazon, that allows developers to build web applications in the cloud without the hassle of managing infrastructure. MongoDB maintains a list of supported platforms at https://docs.mongodb.org/ecosystem/platforms/.

MongoDB is supported in many popular languages. A quick visit to MongoDB's drivers page at https://docs.mongodb.org/ecosystem/drivers/ shows that, as of the time of writing this book, MongoDB has supported drivers for C, C++, C#, Java, Node.js, Perl, PHP, Python, Motor, Ruby, and Scala. Additionally, community-supported drivers for Go and Erlang, undoubtedly, may will be on the way.

Data model

Because MongoDB's data model is based on JSON, it is ideal for use in web applications. JSON output can be consumed directly through frontend JavaScript and JavaScript frameworks such as AngularJs and others.

Because JSON is an object-oriented data format, the data works well with languages that are object-oriented themselves. The data structures can be modeled in the software you're writing very easily.

Popularity

As a developer, the popularity of the tools you are using is relatively important. For one thing, unpopular frameworks don't get the attention from development communities that popular ones get. Using a popular open source tool ensures that there is active development going on.

This extends to things such as books and learning resources, platform availability, and language support.

Popularity can also be an indication of quality or, at least, the quality of fit for popular types of applications. MongoDB has become very popular in Big Data circles, where unstructured data is the bread and butter or day-to-day operation. However, MongoDB really shines when it comes to some of the most popular types of web applications - such as CMS and geo-spatial data.

MongoDB is extremely popular. According to the 2015 press release from MongoDB (https://www.mongodb.com/press/mongodb-overtakes-postgresql-4-most-popular-dbms-db-engines-ranking), MongoDB has surpassed PostgreSQL as the fourth most popular database. As of the press release, it was the only non-relational database in the top five. According to the same release, MongoDB has grown over 160% in popularity over the previous 2 years.

MongoDB is being used more and more in much wider places than many other databases. All indications are that it is going to be around, and be supported on all of the most popular platforms, for a long time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.59