I need help please
I have a collection with 170k records and when I run the mongo db find function it takes too much time to send me the response (too much time )
any solution please ?
You will need to be more specific for help.
- Do you have indexes
- I’m assuming you aren’t trying to return 170k documents, so can you share the query you are running
- How many docs do you want returned?
- Deployment Type and MongoDB Version?
- What is to slow?
The collection name is products
const products = await Product.find()
I want to return all records
What is your use case that you need all the documents? This will never be efficient, this is the same as doing a select * in SQL which is never recommended.
I’m fetching all products in prouducts page
so what do you recommand ? using panigation ?
and how to make indexes ? to prevent mapping all documents in each query
I would suggest pagination to limit the # of results that are returned this will allow it to be quicker. If you think of a website that has products they only give you certain # of elements at a time.
Also, the best case scenario is to filter or query the data somehow, to bring down the size of the documents that are returned. For example do you need all products or just products in a certain category? Or products that have a certain manufacturer? This might not fit your use case but filtering on fields will allow you to create indexes. If you are returning everything then you can’t have an index.
Pagination is your best bet here, otherwise you’re gonna transfer 20Mb of data each time this page loads if you truly want to list every product each time.
Something like this tutorial and documentation is probably a good starting point.