🙅🏼‍♀️ Mistakes I made when I started Nodejs + MongoDb
Tags: webdev, javascript, node, beginners
Published: 2022-06-04 10:12:49

## 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](https://nodejs.dev/learn)

## 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](https://dev.to/aryclenio/how-to-setup-a-nodejs-project-with-typescript-4ehi) from [@Aryclenio Barros
](https://dev.to/aryclenio) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3rcy44vdc3m4jz2ht8u4.png)

Our controller.js file
![wrong controller.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93e51r7ybcuf0d424ash.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vj29vhcyh65czdpzodj.png)

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

![Right controller.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4oje6a1ldpytrugkdvie.png)

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](https://medium.com/@shriharimohan/dont-forget-to-do-this-if-you-re-using-mongodb-indexing-74470d269e62) or [MongoDb Indexing - devTo ](https://dev.to/shrihari/dont-forget-to-do-this-if-youre-using-mongodb-j36)

## 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](https://www.mongodb.com/docs/manual/core/write-operations-atomicity/)

---

📝 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 ](https://dev.to/shrihari/mistakes-i-made-when-i-started-with-angular-applies-to-any-web-framework-180c)

🕸 Are you a budding web developer and in need of some cool css websites to learn from Visit [Colors & Design](https://dev.to/shrihari/colors-design-inspiration-websites-for-new-comers-1lcb)


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](https://docsai.app/) - 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](https://docsai.app/pricing) Just contact me for personalized pricing !

![docsAi](https://firebasestorage.googleapis.com/v0/b/shrihari-personal-storage.appspot.com/o/banner-docs-jpeg.jpeg?alt=media&token=45c47666-de2a-4e02-8881-beb6e43ce581)

---