Hey there,
You're right -- we don't currently support a "sort" clause for
distinct(). But there are a few things you can try in the meantime.
First -- noting that mapreduce is currently single threaded and relies
on running javascript code at the server -- you can use a mapreduce to
write a distinct dataset to a temporary collection to be sorted
subsequently. Here's an invocation that simply picks the first
document out of a collision group and drops it into a new collection
(... with a slightly different schema):
db.runCommand({ 'mapReduce': 'nums', 'map': function() { emit(this.q,
this); }, 'reduce': function(k, vs) { return vs[0]; }, 'out':
{ 'replace': 'tmpdistinct' } })
Next, you can query this data and sort as you see fit. While there IS
a sort parameter to mapReduce, it won't do what we need: it sorts the
input rather than the output.
Note that this is only a good option if you're dealing with a ton of
data. If, on the other hand, you're working with data that'll fit in
memory easily, your best bet is to do the distinct or the sort in your
client code.
Hope this helps,
- Brandon
On Oct 3, 10:40 am, Dominik Gehl <
domi...@dokdok.com> wrote:
> Done :-)
>
> Dominik