DEV Community

Shrihari Mohan
Shrihari Mohan

Posted on • Updated on

πŸ™…πŸΌβ€β™€οΈ Mistakes I made when I started Nodejs + MongoDb

1. Starting nodejs without Express

I started directly with Express Framework and have no idea how nodejs work without express ( then ). Its always good to learn nodejs a couple of hours and then start with express just so you know. Its like knowing Angular/React and not knowing DOM.
Learn nodeJs

2. nodeJs + Typescript = nodeTs

You just started nodejs everything is going well , there are couple of routes and controllers.

Suddenly you have 15 Routes, 15 Controllers, 5 Utils and Middlewares. I always say this types comes a long way. Always use types and it will reduce a lot of developer time when there are a lot of files to deal with.

And if someone new/contributor on your team and you introduce them to your codebase. They will thank you for having types. Trust me !

Check out this Blog from @Aryclenio Barros
to setup Typescript. You can also convert your existing codebase to typecscript.

3. Directly passing res and req to controllers.

This is not a mistake its just a preference to make the code readable. Showing a code sample using Express Framework!

Our route.js file
wrong route.js

Our controller.js file
wrong controller.js

Instead of returning a response inside controller. Let me tell you what is a route and controller in my prespective.

A route for me will handle all the route related things such as query params , request.body and responses.

So our modified route.js

Right route.js

A controller for me will handle only database related operations and just return the results.

Our modified controller.js

Right controller.js

Its a preference that makes our life easier seperating route operations and db operations.

4. Indexing in mongoDB

If you're new to database design , there is a special feature called indexing which will make our querying times faster when dealing with the large collections.

I found indexing because of firebase and implemented in my mongoDb projects. Firebase will not let you run queries if the field is not indexed.

Learn more about MongoDb Indexing - Medium or MongoDb Indexing - devTo

5. Atomic Operations

Lets say there is a requirement where per request you have to modify 2 documents at a same time.

Either Both the documents should be modified or none of them should be modified. This makes our data consistent.

This implementation can be done in mongoDb with the use of atomic operations. Learn more


πŸ“ Bookmark this if you are going to start with Nodejs + MongoDB.

If you're starting with angular you may also want to know the mistakes I made in angular. Learn More

πŸ•Έ Are you a budding web developer and in need of some cool css websites to learn from Visit Colors & Design

And If you like these type of small articles to boost your knowledge, don't forget to follow on dev.to, It motivates to write more and contribute open source.

Peace πŸ•Š !


Try Our new product for free!

DocsAI - Create AI support agents with your documents in the most affordable price, starts at 0$. Don't need a bot , but need ai help on your docs just upload and start chating !

Using for a company ? Check out our pricing Just contact me for personalized pricing !

docsAi


Top comments (0)