Here is how I currently do a distinct with MongoDB aggregation framework:

```
db.big.aggregate([ { "$project" : { "first_name" : "$first_name"}} , { "$group" : { "_id" : { "col1" : "$first_name"}}} , { "$limit" : 50000}])

```

And it takes about 3 seconds on a collection that has a little more than 2M documents.
If I run the following query

```
db.big.distinct('first_name')

```

I get pretty much the same result but in less than 1 second.
The issue I have with distinct is that I can not limit it, so if a collection has 1M distinct values for an attribute everything will be returned.
Is there a way to use distinct as it is more performant but also have a limitation on the number of elements that are returned.
I'm using the MongoDB java driver, so I'd need a solution that works with it.