I have a collection of messages stored in MongoDB and I need to keep only the latest 500 records for each pair of users. Users are identified by their sentBy
and sentTo
attributes.
/* 1 */
{
"_id" : ObjectId("5f1c1b00c62e9b9aafbe1d6c"),
"sentAt" : ISODate("2020-07-25T11:44:00.004Z"),
"readAt" : ISODate("1970-01-01T00:00:00.000Z"),
"msgBody" : "dummy text",
"msgType" : "text",
"sentBy" : ObjectId("54d6732319f899c704b21ef7"),
"sentTo" : ObjectId("54d6732319f899c704b21ef5"),
}
// More document examples here...
/* and more... assume it's over 10,000 documents */
I'm thinking of an algorithm that involves:
- Grouping based on user pairs using the OR operator
- Sorting the records in descending order based on timestamp
- Limited to 500 records per user pair
- Extract the array of
_id
values to be preserved - Use these IDs in a new query with
.deleteMany()
and$nin
condition
I've been struggling with this issue and would greatly appreciate any help or guidance. Thank you so much :)