MongoDB: Database of a CRUD RESTful API

Scott Anderson
4 min readSep 14, 2020

--

Of all the pieces of a full stack application, the database could possibly be the most important. The reason being is that it stores the important data which is the main reason for building the application in the first place. And, since there is data being stored, you must be very particular with your database to keep it safe.

MongoDB is one of the most popular databases used by Software Development companies today. So, I figured I do a quick intro to MongoDB to see how it differs from that of a SQL database such as MySQL.

To begin, MongoDB, launched in 2009, is a NoSQL database. You would think that would be No SQL, but actually it’s “not ONLY sql”, so there are still some sql elements. So, right off the bat, we see there are going to be some differences between a database like MySQL and MongoDB.

One of the advantages of MongoDB that sets it apart from a SQL database is that it is a document data model (below), while SQL is relational (above). With a relational db, everything must be mapped with schemas and fields and datatypes. However, with MongoDB, and the document data model, there are no predefined schemas and you can modify it at anytime. That is not to say that MongoDB does not store relationship data, it’s just stored differently than relational db’s. The relational data of a NoSQL db can be nested within a single data structure.

Below are the CREATE, READ, UPDATE, and DELETE methods for our API and how MongoDB compares to mySQL.

CREATE:

mySQL:

CREATE DATABASE <db name>USE <db name>ALTER TABLE <table name> ADD COLUMN (username VARCHAR(23), user_id INT PRIMARY KEY AUTO_INCREMENT);INSERT INTO <table name> (username, user_id) VALUES (’mctesterson’, 0);

MongoDB:

use <db name>db.<collection name>.insert({“username”: “mctesterson”}

Notice above that the creation of tables and databases with mySQL is a bit more tedious. As mentioned earlier, the table will need to be defined with types. Creating this schema does not then add any new information to the table, but instead will need to INSERT INTO our table a new line of information, declaring what we will be updating.

With MongoDB, as long as you are in the database that you are intending to modify, inserting an object into the collection will both create the necessary collection if it is not already created, as well as input the data into the collection.

READ:

Notice below the 2 different appearances within each terminal respectively.

mySQL:

SELECT * FROM <table>

The above is the data that is returned in the terminal for mySQL. Notice that it is relational tables. While below we see a familiar JSON-like object being returned.

MongoDB:

db.<collection>.findOne.pretty()

The ease of looking for collection data within MongoDB is familiar to a JavaScript full stack developer. Being able to use objects that we are used to with a key value pair is much easier than creating the SELECT statement with WHERE conditions. The use of objects also comes in handy when creating updates.

UPDATE:

mySQL:

INSERT INTO <table name> (username, user_id) VALUES (’mctesterson’, 0);

MongoDB

db.<collection>.insert("_id": ObjectID ("5187592745827"), {key1: “value1”, key2: “value2”, …})

The key distinction here when trying to update tables or collections respectively is that with MongoDB, we again only have to pass in the object that we are looking to update, in this case looking to update the object with the ObjectId of ‘5187592745827’ and then pass in what to add or update. No query or JOIN table like you may have to do for SQL databased updates.

DELETE:

mySQL:

DELETE FROM <table> WHERE <row value> = <value>

MongoDB:

db.<collection>.remove({key1: “value1”})

The delete or remove method is similar between the two databases. But again, the ease with MongoDB’s uses of objects creates a slight advantage again as far as familiarity goes for most JavaScript developers.

Conclusion

SQL is a great tool to understand and utilize. And, throughout your Software Development path, will likely come across SQL databases or similar structures (here’s looking at you excel) of relational tables. However, databases like MongoDB are widely used as they are easier to use with their cloud-based component Atlas and have a smaller learning curve as they utilize BSON in their data interchange.

--

--

Scott Anderson

Someone has to build the computers that will one day take over the world, it might a well be me.