I have a collection with multiple site ObjId's, how could I delete the oldest document based on the date field which have the same site objid field and also add a new field of isCurrent to the only record available?


Is this a one time thing or a frequent use-case?

For a frequent use-case, it is important to try to do it with the least number of database requests. But for a one time thing, like a migration to a new schema, we can afford to be a sloppy.

My sloppy approach would be:

  1. $sort by one of the following index site-date, date-site, site, date
  2. $group by site using $max to find the most recent date
  3. $out in a temp collection
  4. read the temp collection to make a bulk write in the source collection that will set isCurrent to all site,date tuple of temp collection
  5. run a delete many on the source collection for isCurrent:{$exists:false}

@EJ_Rojas, if my post solved your issue, please mark it as the solution. If not share any solution you found on your own to help people facing the same issue. This will help keep this forum efficient and useful to all.