Mongoose Document Position Finder

JS
S
JavaScript

Simple script to find the mongodb Index for a specific ID . This can have several use cases, this one in specific was used to find the last ID that was processed on a failed maintenance task, so we can resume the execution from that point forward.

1function positionFinder() {
2  console.log('finding position during 2018 for a specific id')
3  // Pointers
4  const begOfMonth = moment().utc().year(2018).month(0).startOf('month').toISOString();
5  const endOfMonth = moment().utc().year(2018).month(11).endOf('month').toISOString();
6  const query = { createdAt: { $gte: begOfMonth, $lte: endOfMonth } };
7
8  //var query = { $and: [ { "createdAt": { $gte: ISODate("2018-01-01T00:00:00.00+0000") } }, { "createdAt": { $lt: ISODate("2018-12-31T23:59:00.00+0000") } } ] };
9  //var count = Telemetry.find(query).count();
10  //console.log('got count', count);
11
12  var quarterRange = 3333;
13  var quarter1 = quarterRange;
14  var quarter2 = quarterRange * 2;
15  var quarter3 = quarterRange * 3;
16
17  // Scan
18  let docIndex = 0;
19  Telemetry.find(query).cursor().eachAsync(function (doc) {
20    docIndex++;
21    console.log('it position', docIndex);
22    if (docIndex === quarter1 || docIndex === quarter2 || docIndex === quarter3) {
23      console.log('Quarter Split ID Found', doc["_id"]);
24    }
25    if (doc._id == '5c0a824230b58d000ff0f703') {
26      console.log('last id was on pos', docIndex) 
27    }
28  });
29}

Created on 2/5/2019