Updated December 9, 2023
Web Applications using MongoDB – MongoDB is among the best tools for serious web developers. This article on MongoDB will look at everything you need to know to build web applications with MongoDB. But before we begin, let’s look at the popular tool.
Introduction to MongoDB
In the simplest terms, MongoDB is a cross-platform document-oriented NoSQL database that uses JSON-like documents using dynamic schemas, called BSON documents, instead of following the conventional relational database (RDB) structure.
That’s a lot of terms that you may not be familiar with, so let’s break them down first.
A document-oriented database functions on the basic concept that a data document contains ‘metadata,’ which are values used to identify the data type and make it easier to store and retrieve when needed. For instance, let’s look at the following two lines of data:
- John Hancock
- Architect
These two lines should give you an idea of what this data refers to as a person’s name and occupation. A machine, however, cannot make out this distinction. It needs to be told about the type of data.
This type can be stored in a key-value store separate from the document in an RDB. However, you can determine the data type with a document-oriented database as you type the code. So you end up writing a bit like this:
- <name> John Hancock </name>
- <occupation> Architect </occupation>
And there you go; the data type has been defined right within the document. This gives flexibility not present in RDBs, which is why document-oriented databases are famous for web applications.
After all, web applications are subject to regular changes and updates. Of course, RDBs have their advantages too, and document-oriented databases are not perfect for every situation, but let’s not digress.
Let’s move on to JSON, BSON, and schemas. JSON refers to JavaScript Object Notation, an open standard data format that can work with languages other than JavaScript.
A schema is the structure of the database, defined as tables in the case of RDBs. Since document-oriented databases do not use such tables, they are essentially structure-less or schemaless.
BSON stands for Binary JSON, the data storage format for the MongoDB database. That’s all you need to know about it at this stage.
Now, with the basics out the way, let’s look at why MongoDB is a good choice for making web applications:
Why Go for MongoDB?
There are several reasons for the popularity of MongoDB for building web applications. MongoDB is not a perfect tool; far from it. MongoDB has several challenges and is unsuitable for all web applications. But MongoDB works well when it comes to web applications for the following reasons:
1. MongoDB is schemaless
The MongoDB database can hold collections of documents, each with its own size, content, and number of fields. You can go back and change the key values or add and delete them as you want to.
This also makes the structure of a single object clear and straightforward, and MongoDB is also easy to scale out when needed.
2. MongoDB is general-purpose
The database can be used for several web applications, including customer data management, content management, data hubs, mobile apps, the Internet of Things, Big Data, product and asset catalogs, and database-as-a-service.
3. MongoDB supports auto-sharding
Relational databases generally scale vertically, in which a single server hosts the complete database. This can limit the scale of operation, with the solution being to add horizontally and add servers rather than concentrating capacity on a single server.
Sharing a database is done to make hardware act as a single server and avoid the limitations of individual servers. Developers must deploy multiple relational databases across machines, develop application code to distribute the data and queries, and aggregate the results.
MongoDB and other NoSQL databases support auto-sharing, which means they automatically spread data across various servers without the application being made aware of the composition.
MongoDB is beneficial with cloud computing. As a developer, you don’t have to build expensive and complex platforms to support web applications but can focus on writing the application code.
Few More Terms to Learn about MongoDB
Alright, so we have now covered the basics and benefits of MongoDB. Before we get our hands dirty, let’s look at a few more terms that you should be familiar with:
- Database: A database contains collections, each with files on the file system. A typical MongoDB server will have several databases.
- Collection: A group of MongoDB documents is called a collection, the equivalent of a table in RDBs. A single database will have a collection, and each document will have different fields. Unlike tablets, collections do not have a schema.
- Document: A document is a bunch of key-value pairs with a dynamic schema, meaning that different documents in the same collection can have different fieldsets and structures.
Building Web Applications
The web application built for this particular MongoDB guide is an RSS aggregator like Google Reader. The web applications will have two key components: A REST API and a Feed grabber.
To understand a REST API, you must first know a little about the MEAN stack. The code for a feed grabber can be found even on the GitHub directory.
Understanding the MEAN Stack
The MEAN stack stands for the following:
- MongoDB: The database used for the web application, storing data in a flexible, JSON-like format. They are commonly used to store and retrieve data for web applications.
- Express.js: A web application framework for Node.js. Express.js simplifies the process of building robust and scalable web applications on the server side. Express.js provides a set of features for web and mobile application development.
- AngularJS (now replaced by Angular): A front-end JavaScript framework developed by Google. Angular creates dynamic, single-page web applications with rich user interfaces. Angular allows developers to create reusable components and handle client-side logic efficiently.
- Node.js: It is a server-side JavaScript runtime environment. Node.js allows you to run JavaScript code on the server, enabling the development of scalable and high-performance server-side applications.
The Representational State Transfer (REST) API is a lighter-weight alternative to XML-based APIs. We will create it for the web applications discussed here instead of using an HTML user interface. It can serve as the base for any interface type despite not having a user interface.
REST functions on a client-server model. The client sends HTTP commands to the HTTP server, along with variable parameters encoded in the URL describing the target object of the action. The server then replies with the JSON and result code.
MongoDB and JavaScript react well with JSON, making the MEAN stack especially suitable for web applications.
CRUD
The acronym CRUD stands for creating, reading, updating, and deleting. Post, get, put, and delete HTTP methods correspond to common database operations.
Building a REST API
Let’s begin by creating a REST API and developing the foundation for building web applications. The application could have a web-based version or be designed for specific platforms like Android, iOS, or others. To begin with, let’s list down the things that the app will be able to do:
- Allow users to create an account.
- Please enable them to subscribe or unsubscribe to feeds.
- Please allow them to read and mark feed entries as read or unread.
- Track RSS feeds for monitoring.
- Store user information in their respective accounts
- Track user feed subscriptions
- Track a feed entry already read by a user
Data Modeling
Based on the requirements, the web applications will need four collections for managing the information:
- User collection
- Feed collection
- User-feed entry mapping collection
- Feed entry collection
Here is a closer look at each one:
Feed Collection
The following JSON is a good example of modeling a feed collection:
{ "_id, ObjectId("523b1153a2aa6a3233a91412")
"requiresAuthentication": false,
"permanentlyRemoved": false,
"feedURL": "http://feeds.reuters.com/reuters/topNews",
"title": "Reuters",
"bozoBitSet": false,
"enabled": true,
"etag": "4bL78iLSZud2iXd/vd10mYC32BE",
"link": "http://www.reuters.com/ ",
"permanentRedirectURL": null,
"description": "Reuters news feed” }
The JSON document above is an RSS feed for Reuters, tracking information about the latest news published on the website. You see a lot of fields here, but the most important ones for the web applications to function are the URL of the feed and the feed description.
The URL is important for fetching content from the right source, and the description is essential because it summarizes the feed.
The rest of the fields, meanwhile, are for internal use. You could add more or remove some of them as you feel like. Right at the top of the document is the _id field. Every document must have this field, and the ID must be unique.
In the above example, an ID has been created manually for the field. If you miss this out, MongoDB will automatically make one.
Tracking and Collecting Feed Entries
If you want to track more news feeds, you can add more using the above format. Once you add the feeds, it is time to track and collect them. Given below is a document for feed entry collection:
{ "_id": ObjectId("523b1153a2aa6a3233a91412"),
"description": "Martin Shkreli, the poster boy for prescription drug price increases who was arrested for securities fraud this week, has stepped down from his post as chief executive officer of Turing Pharmaceuticals Inc, the company said on Friday”,
"title": "Turing Pharma names chairman to replace Shkreli as CEO",
"summary": "Turing Pharma names chairman to replace Shkreli as CEO”,
"content": [{ "base": "http://www.reuters.com/",
"type": "text/html",
"value": ”LOTS OF HTML HERE",
"language": "en" }], "published Date": ISODate("2015-12-18T18:13:58+0000"),
"link": " http://www.reuters.com/article/us-usa-crime-shkreli-ceo-idUSKBN0U122R20151218",
"feedID": ObjectId("523b1153a2aa6a3233a913f8") }
Once again, you can see the _id field filled in and other fields such as the description, summary, and title. The content field uses an array, and MongoDB allows arrays to store documents. Holding all the information together, this sub-document arrangement can be beneficial.
Please take note of the feedID; it has the same ObjectId type and the value of the _id as the earlier feed entry document for the Reuters feed. This offers a referential model, so you can query the feed collection on the same ID value to return the Reuters document.
Tracking the User
Here is an example document for keeping track of users:
{ "_id" : ObjectId("54ad6c3ae764de42070b27b1"),
"active" : true,
"email" : "[email protected]",
"firstName" : "John",
"lastName" : "Hancock",
"sp_api_key_id" : "6YQB0A8VXM0X8RVDPPLRHBI7J",
"sp_api_key_secret" : "veBw/YFx56Dl0bbiVEpvbjF”,
"lastLogin" : ISODate("2015-01-07T17:26:18.996Z"),
"created" : ISODate("2015-01-07T17:26:18.995Z"),
"subs" : [ ObjectId("523b1153a2aa6a3233a913f8"),
ObjectId("54b563c3a50a190b50f4d63b") ], }
The user has three key attributes: first name, last name, and email address. You can use two elements with the Stormpath user management API: sp_api_key_secret and sp_api_key_id.
The subscription array, subs, tells which feed users are subscribed to.
Feed Entry Mapping
The final collection enables the mapping of users to feeds and tracking of the feeds that have been read:
{ "_id" : ObjectId("523b2fcc054b1b8c579bdb82"),
"read" : true,
"user_id" : ObjectId("54ad6c3ae764de42070b27b1"),
"feed_entry_id" : ObjectId("523b1153a2aa6a3233a91412"),
"feed_id" : ObjectId("523b1153a2aa6a3233a913f8") }
The feed uses a basic true/false Boolean to indicate whether it has been read or not. Now, the user should be able to perform the following functions:
- Creating an account
- Subscribing or unsubscribing to feeds
- Reading feed entries
- Marking feeds or entries as read or unread
- Reset the password
Here is a list of how to map these operations to HTTP routes and commands.
Route | Command | Description | Variables |
/user/enroll | POST | Register a new user | firstName lastName password |
/user/resetPassword | PUT | Password Reset | |
/feeds | GET | Get feed subscriptions for each user with description and unread count | |
/feeds/subscribe | PUT | Subscribe to a news feed | feedURL |
/feeds/entries | GET | Get all entries for feeds the user is subscribed to | |
/feeds/<feedid>/entries | GET | Get all entries for a specific feed. | |
/feeds/<feedid> | PUT | Mark all entries for a specific feed as read or unread | read = <true | false> |
/feeds/<feedid>/entries/<entryid> | PUT | Mark a specific entry as either read or unread | read = <true | false> |
/feeds/<feedid> | DELETE | Unsubscribe from this particular feed |
The standard approach for sending sensitive details like passwords in a production environment is to use Secure HTTP.
Next Steps: Real-world Authentication
User authentication is essential for securely managing users, passwords, and resets. There are several ways to authenticate user accounts.
For instance, you could use a Passport Plugin with Node.js, which helps authenticate Facebook, Twitter, and social media accounts.
The above example depends on using Stormpath, a REST API that supports authorization and authentication via API keys and offers user management-as-a-service.
Recommended Articles
Here are some articles that will help you get more detail about the Web Applications using MongoDB, so go through the link.