A crucial step in building any web application is to choose the location for storing data. Databases are all the more critical for e-commerce applications. A database needs to be flexible enough to adapt to the ever-growing needs of the future while fitting the current products catalog. Scalability is another feature of paramount importance that can allow any business to grow or shrink. How many orders per minute does the application handle? What's the average response time of users' load peaks and valleys? How long does it take to find a product in a large pool? Will today's schema meet tomorrow's demands? These are a few of the many questions that affect the choice of database.
For around 40 years, relational database technologies (RDBMS) have governed the enterprise world. However, traditional databases often fall short in terms of scaling web applications to millions of users around the world. Today, leading internet companies (Amazon, Google, Facebook, and others) have developed NoSQL technologies to overcome the limitations of the RDBMS. NoSQL is focused on providing scalability, performance, and high availability. It has a growing number of test cases such as big data, big users, cloud computing, and the Internet of Things (IoT).
MongoDB is one of the most popular NoSQL databases. It has been adopted by a number of major websites such as Craigslist, eBay, Foursquare, The New York Times, and others. MongoDB provides a flexible data model and an expressive query language, and is highly scalable. We are going to use MongoDB to build our application. This chapter will get us started with Mongoose, which is a NodeJS driver for MongoDB. In further chapters, we are going to explore more advanced features to scale MongoDB. The following topics will be covered in this chapter:
MongoDB is an open source, cross-platform, document-oriented database. Instead of using the traditional table-based rigid schemas, MongoDB favors JSON-based documents with dynamic schemas.
In the first chapter, we installed MongoDB. There are two main executables that come with the installation:
Execute the mongo
shell, and you will see the Command Prompt. It is similar to the NodeJS Command Prompt in the sense that it is a JavaScript environment. We can define custom JS functions apart from the core functions that are also available. For instance, the following Math
module and parseInt
are available:
mongo> db.products.insert({ ... title: 'Cellphone', ... stock: parseInt(Math.random()*100), ... price: Math.random()*100 ... }) WriteResult({ "nInserted" : 1 })
For those of us who are already familiar with the SQL world, this section will be useful for translating this knowledge into the MongoDB way of performing similar actions.
The following basic concepts and terms will help us speak the same language through this chapter:
One of the advantages of MongoDB is its powerful querying language. Here are a few examples of common SQL queries and the way they translate to MongoDB:
Being schema-less gives much flexibility, since we can add new fields without having to change the previous data.
Aggregators perform a calculation on the data, and usually reduce it to numeric values or filter/sort out data.
SQL |
MongoDB |
Description |
---|---|---|
SUM(column) / COUNT(*) |
$sum |
This sums/counts all rows/fields |
WHERE / HAVING |
$match |
This filters documents based on the query passed |
GROUP BY |
$group |
This groups rows/documents by an expression. |
ORDER BY |
$sort |
This sorts the data |
Examples | ||
SELECT SUM(price) FROM products AS total; |
db.products.aggregate([{ $group: { _id: null, total: { $sum: "$price" } } }]) | |
SELECT title, SUM(price) FROM products GROUP BY title HAVING title == 'Product'; |
db.products.aggregate([ { $match: {title: 'Product' }}, { $group: { _id: '$title', total: { $sum: "$price" } }} ]) |
This adds all products by title and shows only the one where the title is |
3.145.69.185