|
This can be done using setUnion operator as well:
db.unique.find()
|
{ "_id" : ObjectId("56b3c1baa9a5419494b42e28"), "a" : [ 1, 2, 2, 2, 3, 3, 4, 4, 5 ] }
|
{ "_id" : ObjectId("56b3c1c2a9a5419494b42e29"), "a" : [ 1, 2, 2, 3 ] }
|
agg@MongoDB Enterprise :27017(3.2.1) > db.unique.aggregate({$project:{a:{$setUnion:"$a"}}})
|
{ "_id" : ObjectId("56b3c1baa9a5419494b42e28"), "a" : [ 4, 3, 5, 2, 1 ] }
|
{ "_id" : ObjectId("56b3c1c2a9a5419494b42e29"), "a" : [ 3, 2, 1 ] }
|
|
|
While it's not pretty, you can do this as of 2.5.3 using the $setDifference operator:
> db.test.drop()
|
true
|
> db.test.insert({a:[1,2,2,2,3,3,4,4,5]})
|
> db.test.find()
|
{ "_id" : ObjectId("526fe651e12aa335e040fa7a"), "a" : [ 1, 2, 2, 2, 3, 3, 4, 4, 5 ] }
|
> db.test.aggregate({$project: {a: {$setDifference: ['$a', []]}}})
|
{ "_id" : ObjectId("526fe651e12aa335e040fa7a"), "a" : [ 1, 2, 3, 4, 5 ] }
|
|