Alright, so I'm building an application based in Node.js and I am using mongoose to handle my connection to mongodb. I have an endpoint that is such:
getTestStream : function(req, res, conditions, callback) {
Activity.find()
.limit(1000)
.run(function(err, activities) {
if (err){
util.sendError(req, res, "Query Error", err);
} else if (activities) {
res.send(activities);
} else {
util.send('nope');
}
});
}
For some reason this call takes 700ms+ to complete. The same call without even applying a limit made from mongodb shell returns in about 4ms. It seems like such a simple query, so what's slowing it down so much? I'm guessing I've missed something obvious in configuration somewhere, but I have no idea.
Thanks to anyone who can help on this.
Other info:
mongoose@2.6.0
mongodb@2.0.4
node@0.6.9
After experimenting for a while, I've found several contributions to slowness, hopefully this helps anyone with a similar issue:
Using these techniques I can now process 4000 records in less time than I was processing 1000 before. Thanks for anyone who commented, and special thanks to Gates VP for pointing out that mongoose wasn't really a good fit for this kind of call.
The same call without even applying a limit made from mongodb shell returns in about 4ms.
The shell applies a limit of 30 or so by default. Try doing from the shell with an actual limit?
Also, you may want to try a .explain()
from the Shell.
If none of that works, then you can take @Kyle Banker's suggestion and check out the profiler.
checkout ensureIndex This will speed up your search