mongodb php+distinct+sort+"how i can create map\reduce function"

426 views
Skip to first unread message

lion69

unread,
Jan 1, 2012, 4:08:48 PM1/1/12
to mongodb-user
hi to all))
i have documents like this
[table] = .......
[_id] = ....

i need get distinct of "table" but i want to sort it by _id
can anybody help me to create map\reduce funtion in php to this...?

Eliot Horowitz

unread,
Jan 2, 2012, 1:13:52 AM1/2/12
to mongod...@googlegroups.com
Not entirely sure what you mean.
Can you send an example document and what you want the result to be?

> --
> 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.
>

lion69

unread,
Jan 2, 2012, 4:20:25 AM1/2/12
to mongodb-user
documents:

[table]= a
[_id] = 1

[table]= b
[_id] = 5

[table]= r
[_id] = 2

Result:
a r b - its sorted by _id

now my distinct is this:
public function distinct($collection, $key, $query) {
return $this->db->command(array('distinct' => $collection,
'key' => $key, 'query' => $query));
by its return an array and i cant sort it, or limit it, or... in a
word, bad that this function not return a MongoCursor)
}

Derick Rethans

unread,
Jan 2, 2012, 7:58:49 AM1/2/12
to mongodb-user

Sadly, we can't do that in the driver, because commands "will return a
single document with the command results"
(http://www.mongodb.org/display/DOCS/Commands). So your only way for
sorting would be to do this in your application. For example with
http://docs.php.net/uasort

cheers,
Derick

--
http://mongodb.org | http://derickrethans.nl
twitter: @derickr and @mongodb

lion69

unread,
Jan 2, 2012, 9:27:50 AM1/2/12
to mongodb-user
yes, its sadly, but i think it can do with map\reduce, write some js-
function and sort it, but i dont know how it make

Derick Rethans

unread,
Jan 2, 2012, 9:46:31 AM1/2/12
to mongodb-user
On Mon, 2 Jan 2012, lion69 wrote:

> yes, its sadly, but i think it can do with map\reduce, write some js-
> function and sort it, but i dont know how it make

Actually, It's fairly trivial to do this in your application (with PHP's
uasort()). I don't quite understand why you are insisting to do this on
the server side? It's probably going to be easier to do this in PHP, but
in order to help you I would need to see some (simple, but working)
example code.

Also, in a future MongoDB server version, the new aggregation
framework would allow you to group by/distinct and sort.

Reply all
Reply to author
Forward
0 new messages