0

I am fetching all data from collection and that operation is taking time. And there are chances that data may increase in near future. So I need to optimize the query. But I have no filter parameters, I want all data as it is.

collection().find.toArray(function(err, data){
            if(err){
                console.log("unable to fetch data from collection",err);
                res.status(400).json({error : 'Unable to fetch data'});
            }
            else{
                console.log("data send",data.length);
                res.status(200).send(data);
            }
        })

How can I optimize this, so that It will not take much time.

Thanks Mangesh

Mangesh Tak
  • 346
  • 1
  • 6
  • 22
  • Possible dupe of https://stackoverflow.com/questions/21626896/nodejs-mongodb-native-find-all-documents – JohnnyHK Jul 18 '18 at 18:38
  • I tried solutions from that comment, but even stream() is taking same time. Any other way? – Mangesh Tak Jul 18 '18 at 21:42
  • 1
    Then you may not be able to speed it up. It's usually a bad idea to return all of a large collection's data in a single API call. – JohnnyHK Jul 18 '18 at 23:37

0 Answers0