IsaacLevon
IsaacLevon

Reputation: 2570

MongoDB: Find documents but drop duplicates by key

What I want is to find documents but drop duplicates by key and retrieve only the updated document (by a date field).

So for this collection:

{
    "key": "a", 
    "time": ISODate("2021-10-20T00:00:00.000Z")
},
{
    "key": "a",
    "time": ISODate("2020-10-20T00:00:00.000Z")
},
{
    "key": "b",
    "time": ISODate("2020-10-20T00:00:00.000Z")
},
{
    "key": "b",
    "time": ISODate("2019-10-20T00:00:00.000Z")
}

We will get the following docs:

{
    "key": "a",
    "time": ISODate("2021-10-20T00:00:00.000Z")
},
{
    "key": "b",
    "time": ISODate("2020-10-20T00:00:00.000Z")
}

How can I do that?

Upvotes: 0

Views: 43

Answers (1)

Yong Shun
Yong Shun

Reputation: 51125

  1. $sort - Order the documents by key ASC and time DESC.
  2. $group - Group by key and take the first document as data.
  3. $replaceWith - Decorate output document with data.
db.collection.aggregate([
  {
    "$sort": {
      "key": 1,
      "time": -1
    }
  },
  {
    $group: {
      _id: "$key",
      "data": {
        $first: "$$ROOT"
      }
    }
  },
  {
    "$replaceWith": "$data"
  }
])

Sample Mongo Playground

Upvotes: 1

Related Questions