This code loops through a given range of dates and for every 5 minute iteration pulls up the last known records of the users in the database relative to that timestamp.I'm trying to find a way to optimize this query. Right now it seems to be slow, perhaps because it loops over the entire db each time it runs. Any ideas?// cleaning / sortdb.Data.createIndex({_created_at: -1});// help : hackhands.com/vibhorvar start = new Date(2013, 2, 13, 0, 0, 0);var end = new Date(2013, 2, 13, 23, 0, 0);while (start < end) {    var newDate = start.setMinutes(start.getMinutes() + 5);    start = new Date(newDate);    db.getCollection(_User).find().forEach(function (myDoc) {        var a = db.Data.find({user: myDoc.username, _created_at: {$lt: start}}).sort({ _created_at: -1}).limit(1);        if (a.length() > 0) {            print(a[0][battery])        }    });    print(start)}
Locate enteries to the nearest date selected
javascript;database;mongodb