How can I remove a duplicate object from a MongoDB array?

Posted by andrewrk on Stack Overflow See other posts from Stack Overflow or by andrewrk
Published on 2014-08-21T01:38:55Z Indexed on 2014/08/21 16:20 UTC
Read the original article Hit count: 187

My data looks like this:

foo_list: [
  {
    id: '98aa4987-d812-4aba-ac20-92d1079f87b2',
    name: 'Foo 1',
    slug: 'foo-1'
  },
  {
    id: '98aa4987-d812-4aba-ac20-92d1079f87b2',
    name: 'Foo 1',
    slug: 'foo-1'
  }
  {
    id: '157569ec-abab-4bfb-b732-55e9c8f4a57d',
    name: 'Foo 3',
    slug: 'foo-3'
  }
]

Where foo_list is a field in a model called Bar. Notice that the first and second objects in the array are complete duplicates.

Aside from the obvious solution of switching to PostgresSQL, what MongoDB query can I run to remove duplicate entries from foo_list?

Similar answers that do not quite cut it:

These questions answer the question if the array had bare strings in it. However in my situation the array is filled with objects.

I hope it is clear that I am not interested in a query; I want the duplicates to be gone from the database forever.

© Stack Overflow or respective owner

Related posts about mongodb

Related posts about aggregation-framework