NextJs Mongodb NodeJs | best way to filter products by category

Issue

I’m new to the MERN Stack and I’m building my first project but I’m stuck on finding a way to filter products by category
I was thinking of adding a "Category" field for each item in MongoDB but I had no idea how to show only this specific category
If you could give me a general idea, maybe an example or even a resource to learn from… because on YouTube I’ve seen A LOT of videos where they were filtering Static Products but in my case, I have a database, and I couldn’t find anything that helped…

Solution

Yes, you can add a category field and then just make a query for documents with some particular category.

const cursor = db.collection('inventory').find({ category: 'red' });

Here you are querying your collection "inventory" for the documents which have value "red" in their category field.

Here is MongoDB documentation about queries. Or your might want to use an ORM system, like prisma – it makes things little less complex.

Answered By – VladSofronov

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.