The MongoDB shell uses the javascript mechanism, and a few months ago I remember in javascript some problem using the var inner loop, since we use int inside java.
try removing var from the loop statement
for (i = 0; i <= 6; i=i+0.12){
var n = i + 0.12;
db.test.aggregate([
{ $sort: {'deviation': -1}},
{ $unwind: '$foo' },
{ $match: { 'foo.km': {$gt: {n}, $lt: {i}}}},
{ $limit: 1}
])
}
Also remember that js is asynchronous by default, so it may not wait for the aggregate to complete and move on to the next iteration.
source
share