How can I remove a duplicate object from a MongoDB array?
- by andrewrk
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:
http://stackoverflow.com/a/16907596/432
http://stackoverflow.com/a/18804460/432
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.