0

Since mongodb I work recently tell me please there is a collection with such records

Code
{
    "_id": "test1",
    "clientid": "main",
    "date": ISODate ("2018-04-21T09: 10: 34.705Z")
}
{
    "_id": "test2",
    "clientid": "main",
    "date": ISODate ("2018-04-21T09: 10: 34.705Z")
}
{
    "_id": "test2",
    "clientid": "main",
    "date": ISODate ("2018-04-21T08: 10: 34.705Z")
}

I need to delete all but the one with the maximum date and at the same time two of them have the same date. without adding new specific column to sort how can be done it For example, if the identical records by date >1000 how to delete all who have a date less and leave one with a maximum date? Using find (). Sort (date: -1) .limit (1) Thank you!

DevBR
  • 1
  • 1
  • can you give some understandable example and clear idea, what you want to keep waht else you want to delete exaclty.? – Saikat Chakrabortty Apr 21 '18 at 11:03
  • The process is to run through an aggregation to get the "distinct" items for whatever value you want ( here it is `"clientId"` ), keep the array of matches and discard the older two by looping the results to a `.remove()` ( or better `deleteOne()` or `deleteMany()` ) command. It's been demonstrated here for a long time. – Neil Lunn Apr 21 '18 at 11:24
  • Ok i see basic delete duplicates work fine But how not delete last one and save latest by date – DevBR Apr 21 '18 at 14:10

0 Answers0