Why is my mongodb call so slow?

database, javascript, mongodb, mongoose, node.js

Solution

After experimenting for a while, I've found several contributions to slowness, hopefully this helps anyone with a similar issue:

- The objects I'm requesting are large, so processing them takes some time. For large objects modify the query to only return the fields you need right now.

- Mongoose is useful, but it can really slow down when you request a lot of items, its better to just directly interface with node-mongodb-native if you want speed for a call. (This was about a 50%+ speed increase for my scenario)

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.

Problem

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 ```

Original source

Related problems