Using Node.js with MongoDB part II: A CRUD Exercise for Beginners

Alex Foreman
4 min readJun 27, 2021

Last week, I covered the creation of a very basic Node.js application connected to a MongoDB server. In that blog, I outlined the “C” in CRUD, the “Create” action. If you have not already, I recommend reading the first part, as it covers a number of setup operations not covered here.

This week, I will demonstrate the rest of the CRUD actions. Like last week, I am providing more of a proof of concept than a fleshed-out application. The app doesn’t “do” much, but the Node functions I write successfully manipulate the Mongo database, which I then verify using the Mongo admin software Robo 3T.

For a quick refresher, here is a look at the basic setup to connect to a Mongo server and let the software know you’re creating a database:

How to get started

The rest of the CRUD actions I outline will use this code as a baseline of sorts. I simply comment out the previous function when I move to the next one while leaving the above to maintain the connection.

The variable “db” in the above code signifies the creation of a Mongo database. I then specify which database, the “collection.” The pets database is simply a list of pet names and their corresponding animal species.

A couple of READ methods

The above screenshot includes a couple of different Read methods. Again, these methods are part of the Node.js MongoDB driver API. The first method, findOne, operates as you would expect. It attempts to find one particular database entry, depending on my query. The second example is more specific. I queried the database for all animals “cat”, then used the toArray method to return them all. Below is the information for both returned to the console.

Console.log returns from Read methods

Moving to the next CRUD function, the easiest way to show how to Update is to search for a specific entry and make a manipulation. To do this, I grabbed the ObjectID of one of the entries from Robo 3T. When I previously created entries for pets, I only generated an object with “name” and “animal” (type).

The ObjectID is automatically created. According to the docs, it is a 24 byte hex string or 12 byte binary string. You can see an example of this id from the console logs accompanying the Read actions.

Update action

In the above code, I again identify the specific collection to query then use the updateOne method to make a change. This method takes the specific ObjectId. The next part, “$set”, is a field update operator. These docs provide all of the details, but field update operators are methods which allow you to change database entries. They all begin with a dollar sign. Other examples include $rename, as well as number specific ones like $inc (increment), $min, and $max.

One cool thing about the above example is that I did not have to specify the type of animal when I used $set. Because I am providing the ObjectId, MongoDB knows I am talking about a hamster and am changing the name of that hamster.

Not much changes for the final example, the Delete action. Like the find method, you can deleteOne or deleteMany.

Delete example

In the above code, I again call on the “pets” database collection and ask to delete all entries matching cat. When I refresh Robo 3T, all three cat entries have disappeared.

Because there’s a great deal of consistency regarding syntax and naming, you can see that it’s pretty easy to implement all CRUD action types once you’ve got one down.

--

--