mongodb distinct and sort in PHP

626 views
Skip to first unread message

max_paine

unread,
Nov 4, 2011, 7:38:48 AM11/4/11
to mongodb-user
Hi guys,

Here is the situation:

I have collection called reports where I have this structure:

user-id, article-id,timestamp.

I want to select distinced article ids sorted by timestamp descending
and I'm really stuck because I cannot add sort() function to the
result of the distinct command and also I don't know how to add sort
parameter to the command. Any help will be In use! Right now I'm
using really complex code after the single distinct, but it will be
really problem with productivity of the script.

Thanks,

Yasen Yankov

Sam Millman

unread,
Nov 4, 2011, 9:02:53 AM11/4/11
to mongod...@googlegroups.com
You would have to do this with MR at present I believe.

If you emit per article-id and then do your sort on the cursor it should work.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Brandon Diamond

unread,
Nov 4, 2011, 1:54:35 PM11/4/11
to mongodb-user
As Sam pointed out, MapReduce is your best bet. Distinct returns
results in a single document which means you'd have to do sorting in
the client to make this work (also, there's a 16 MB limit to be aware
of).

Here's the distinct documentation for completeness:
http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct

And here's information on using MapReduce:
http://www.mongodb.org/display/DOCS/MapReduce

Hope this helps,
- Brandon

On Nov 4, 9:02 am, Sam Millman <sam.mill...@gmail.com> wrote:
> You would have to do this with MR at present I believe.
>
> If you emit per article-id and then do your sort on the cursor it should
> work.
>

max_paine

unread,
Nov 5, 2011, 5:58:43 AM11/5/11
to mongodb-user

Hey,

I was thinking of map reduce but I don't want to make a collection for
every user.
maybe to distinct with map reduce and to catch from the new
collection but it will need to synch the collections.
Also in php I cannot make map reduce to store in a temporary
collection just for the operation. As I said I have 24 000 users so it
will mean 24 k different collections?
I think normally should be made by finding with sorting and then
reduce ? Something like:
db.reports.find(criteria).sort().distinct() but it does not work.

Regards,
Yasen
Reply all
Reply to author
Forward
0 new messages