I know it’s weird. If you want to see it in “action” run the first part which I’ve stripped lots of stuff out of, to create 4000 records in a temp collection. Then run the second part against it. Try with the [0] commented out first. For me this runs in about 1/20th of a second. When uncommenting the [0] it takes about 8-10 seconds!
var toDate = new Date()
var epochTicks = 621355968000000000
var toTicks = ((toDate.getTime() * 10000) + epochTicks);
var count = 0
var shipNum = 621355968000000000
for (i = 0; i < 4000; i++) {
count = count + 123456789
shipNum = shipNum + 123456789
db.tracking_temp.insert({
"SecondEvent" : {
"Code" : "1111"
},
"Ships" : {
"Name" : "xyz",
"Number" : NumberLong(shipNum)
},
"TrackTime" : [
NumberLong(toTicks + count),
60
]
})
}
…then run this. Doesn’t print or do anything but will show the jump in run duration.
var ships = db.tracking_temp.find({}, {_id:0, 'Ships.Number': 1}).toArray()
var shipsCount = ships.length
for (i = 0; i < shipsCount; i++) {
var shipsNumber = ships[i].Ships.Number
var minTick = db.tracking_temp.find({$and: [{'Ships.Number': shipsNumber}, {'SecondEvent.Code': "1111"}]},
{_id: 0, 'TrackTime': 1}).limit(1)//[0]
print()
}