DEV Community

LS
LS

Posted on

Mongo db index creation

// mongosh

function getLastFridayNYMidnightUTC() {
  const now = new Date();
  const daysSinceFriday = (now.getDay() - 5 + 7) % 7; // Friday = 5
  return new Date(now.getFullYear(), now.getMonth(), now.getDate() - daysSinceFriday, 0, 0, 0, 0);
}

const lastFriday = getLastFridayNYMidnightUTC();
print("Last Friday (NY midnight) =", lastFriday.toISOString());

// Find all docs where lastModifiedTimestamp.dateTime > lastFriday
db.getCollection("som.application").find(
  {
    $expr: {
      $gte: [
        { $dateFromString: { dateString: "$lastModifiedTimestamp.dateTime" } },
        lastFriday
      ]
    }
  },
  { _id: 1, "lastModifiedTimestamp.dateTime": 1 }
).forEach(printjson);

db.getCollection("som.application").find(
  { "lastModifiedTimestamp.dateTime": { $gte: lastFriday } },
  { _id: 1, "lastModifiedTimestamp.dateTime": 1 }
)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)