MongoDB Querying Large Collections MongoDB Querying Large Collections mongoose mongoose

MongoDB Querying Large Collections


I figured this out sort of.

I managed to get the index working with by adding an index to the Mongoose Schema:

timestamp: {    type: Date,    index: true,    default: Date.now}, 

Then using the following function to perform the query.

function getResultsInRange(startDate, endDate) {    if(typeof startDate !== 'undefined' && typeof endDate !== 'undefined') {        Price.find({timestamp: {$gte: startDate, $lte: endDate}}, 'price timestamp exchange')        .sort('-timestamp')        .populate('exchange')        .exec(function(err, prices) {            if(err) {                res.jsonp({'error': err});            } else {                res.jsonp(prices);            }        });        }}   

The above works with up to a 14 day range between startDate, and endDate, although it takes about 20 seconds to run even with the index.