Mongo DB Hello World inside Node JS Express API

Jay (Vijayasimha BR)
3 min readAug 27, 2022

--

some random mongo db screenshot

One more blog post that helps my students in the ongoing 4 month long MERN training batch. This time, about mongo db.

As always, there are two projects, readily available for this post.

Project 1 : MongoDBHelloWorld — this contains some essential stuff about what you can do to play around in the command query shell.

Project 2 : MongoDB_API_CRUD_HelloWorld — this contains a proper API server built with Express Node JS. it provides some endpoints that interact with a cloud based mongo db server. This is the main thing, because, ultimately, you build your API to collect and send data from a real database server.

First step is, you have to install mongo db locally. Useful for you to go ahead and practice some queries and get used to the mongo system.

For example, here are some queries you can and should try.

One thing, I want to share is, unlike regular databases, mongo db literally stores JSON objects. If you don’t already know what and how JSON objects behave, then, I cannot help you.

But, if you know what JSON objects are, then, you know what I am talking about.

This is a blessing and also a pain to work with. That is the primary choice you will be making when you decide to work with mongo db.

Right then, next, here is you might do a simple get endpoint in your API server.

Essentially, first we connect to the mondo db database. Note that mongo db providers ‘drivers’ for the technology stack you are working. I am using the node js driver, of course.

After connection, i build the query object, and fill up some options and such. Once again, all of them are JSON objects. So, please know your JSON before you jump into this.

I want you to pay special attention to this line of code.

await resultOfDb.forEach(function (x) {

You see, with mongo, what you get is a ‘cursor’. You navigate the response using the cursor. You don’t actually get the full collection, in this case. That means, to grab the actual collection of items, you are supposed to navigate through all this.

and this is how a simple POST would look.

Here, I am doing the same thing as before. getting a collection of movies. However, the difference is, this time, I am querying the database to get the movies based on the movie that was queried as part of the POST body.

For example, like you when you have to search for a movie on imdb or any movie website.

The same code will also work if you want to add things to the database. Like adding a movie or adding a new user to your system, or updating details to the database.

Final Note

There is not much else to say here. check the linked project and go build your project.

email communications to affected users.

I have always been an Azure guy. But, you know what, even to activate a free trial, you need to have a credit card.

Indian parents will never share credit cards with their kids man. That’s not going to happen.

This is going to set back learning for a lot of students.

It’s very sad, man :(

I work as a full time freelance coding tutor. Hire me at UpWork or Fiverr or Stack Overflow. My personal website is here. Find more of my art at Behance and Unsplash. Also, I have a Podcast about everyday life. And, a 2nd Podcast, where I talk about freelancing.

--

--

No responses yet