0

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'
}
1

1 Answer 1

2

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"
    }
  }
])
Sign up to request clarification or add additional context in comments.

2 Comments

It works!! tahnksMay I for example delete the duplicate accoriding to the maxium of some parameter
If by "delete duplicate" you mean select specific documents from the group - yes. You can $push all documents into an array and add an extra stage to pick the one you need before the $replaceRoot stage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.