MongoDB requests very slow to return results

MongoDB requests very slow to return results
0.0 0

#1

Hello,

So, I only have a few documents in my Mongo DB. For example, I have this basic find request (see below) which takes 4 seconds to return a 1.12KB JSON, before the component re-render.

	MongoClient.connect(urlDb, (err, db) => {
		let Mycoll = db.collection('Mycoll');

		Mycoll.find({}).toArray( (err, data) => {
			if (err) throw err;
			else{
				res.status(200).json(data);
			}			
		})

		db.close();
	})

Sometimes for the same component to re-render, with the same request, it takes 8 seconds.

Is it supposed to take this long ? I can imagine a user of my app starting to think (“well, that doesn’t work”) and close it before the results show.

Is there anything you could point me to to optimize the performance ? Any tool you would recommend to analyze what exactly causes this bottleneck ?


#2

How much data is stored in that collection? You didn’t place any filters or limits to your find query so your query is returning EVERYTHING form the collection. That may be why its taking so long.

Are you running your database locally or with a 3rd party service like MLab. Could the issue be with them?

Also if it counts for anything, I also ran into a similar issue when developing one of my projects. The first request after starting the database would take forever (3-5) seconds but every request after that was just fine. When I tried it on Linux though, the probelm went away.