MongoDb aggregation delete duplicates according to parameter

I am trying to find out how I can make a mongodb aggregation to delete duplicate documents according to a specific parameter. When I say duplicates I don't mean that the whole document is the same. For example I have the following documents:

{
  'event': 123,
  'element': 'element1'
},
{
  'event': 456
  'element': 'element2'
},
{
  'event': 789
  'element': 'element1'
},
{
  'event': 111
  'element': 'element3'
}

I would like to make an aggregation that if I specify the field "element" will return me all documents without element duplication. I know that the typical question is, ok but if there are two documents with 'element1' Which one do I want? the answer is that I dont mind if it returns me one or the other, this is why I dont know if this is possible

return example:

{
  'event': 123,
  'element': 'element1'
},
{
  'event': 456
  'element': 'element2'
},
{
  'event': 111
  'element': 'element3'
}

It is also OK if it returns me:

{
  'event': 456
  'element': 'element2'
},
{
  'event': 789
  'element': 'element1'
},
{
  'event': 111
  'element': 'element3'
}

Upvotes: 0

Views: 1506

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

It's not deletion by any means. It's selection of documents with unique value in the field.

Such things are done by grouping documents by the field in question. Since you need any of the matching documents you can use $first accumulator:

db.collection.aggregate([
  {
    "$group": {
      "_id": "$element",
      "doc": {
        "$first": "$$ROOT"
      }
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$doc"
    }
  }
])

Upvotes: 2

Related Questions