fetching distinct values in Perl

576 views
Skip to first unread message

Gabor Szabo

unread,
Oct 18, 2010, 3:28:20 AM10/18/10
to mongod...@googlegroups.com
hi,

I am quite new to MongoDB and thus to this list so let me say hi!

I've started to build a small application using MongoDB but now as I
try to fetch a list of distinct values I hit a wall.
I managed to used the mongo client to fetch the distinct values but I
am not sure how should I do it in Perl.
The distinct method seems to be accepted but it returns a
MongoDB::Collection and I don't know how
to fetch the data from there. Besides, I also got an exception.
Sample code:


use strict;
use warnings;
use MongoDB;

my $connection = MongoDB::Connection->new(host => 'localhost', port => 27017);
my $db = $connection->test;
$db->insert({ 'name' => 'a', code => 1 });
$db->insert({ 'name' => 'b', code => 1 });
$db->insert({ 'name' => 'c', code => 2 });

my $distinct = $db->distinct('code');
print "$distinct\n"; # MongoDB::Collection=HASH(0x14f4798)
my $r1 = $distinct->find();
print "$r1\n"; # MongoDB::Cursor=HASH(0x853260)
while (my $d = $r1->next) {
print "$d\n";
}


my $r2 = $distinct->find('code');

print "R2: $r2\n"; # MongoDB::Cursor=HASH(0x853260)
while (my $d = $r2->next) {
print "D: $d\n";
}
# throws an exception:
# not a reference at
/home/gabor/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/MongoDB/Cursor.pm
line 231.

print "done\n";

Kristina Chodorow

unread,
Oct 18, 2010, 9:09:33 AM10/18/10
to mongod...@googlegroups.com
The problem is that there is no distinct helper in the Perl driver.  You're fetching the "distinct" collection.

You need to use run_command (http://search.cpan.org/dist/MongoDB/lib/MongoDB/Database.pm#run_command_%28$command%29) to run distinct (http://www.mongodb.org/display/DOCS/Aggregation?focusedCommentId=4161540#Aggregation-Distinct).



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


mila

unread,
Oct 18, 2010, 9:32:43 AM10/18/10
to mongodb-user
try this...
~~~~~~~~~~~~~~~~~~~~~~~~~~
my $connection = MongoDB::Connection->new(host => 'localhost', port =>
27017);
my $db = $connection->test;
my $coll = $db->test_collection;

$coll->insert({ 'name' => 'a', code => 1 });
$coll->insert({ 'name' => 'b', code => 1 });
$coll->insert({ 'name' => 'c', code => 2 });

my $r3 = $db->run_command([
"distinct" => "test_collection",
"key" => "code",
"query" => {}
]);

print "R3: ",dump($r3),"\n";

for my $d ( @{ $r3->{values} } ) {
print "D: $d\n";
}
~~~~~~~~~~~~~~~~~~~~~~~~~~
mila

Gabor Szabo

unread,
Oct 19, 2010, 8:37:29 AM10/19/10
to mongod...@googlegroups.com


thanks.

Except of the dump($r3) part this works and shows what I needed.
Maybe Kristina could include this as an example in the CPAN distribution.

regards
Gabor

Kristina Chodorow

unread,
Oct 19, 2010, 9:06:57 AM10/19/10
to mongod...@googlegroups.com
Good idea, I added a section on distinct to the Examples pod.


mila

unread,
Oct 20, 2010, 2:36:37 AM10/20/10
to mongodb-user
On Oct 19, 2:37 pm, Gabor Szabo <szab...@gmail.com> wrote:

> Except of the dump($r3) part this works and shows what I needed.

sorry, i forgot to include "use Data::Dump q(dump)" in my code
snippet.

mila

Gavin Colborne

unread,
Sep 27, 2017, 2:43:46 AM9/27/17
to mongodb-user
Hello Mila, Kristina and Gabor,

Thanks your example really helped me with selecting distinct documents from my Mongo collection.

I am a little stuck with the "query" syntax on the run_command.

I need to simply get the value of one other column in the same collection (pageURL) - what do I use in the "query" to retrieve this?

Here is my sample code:

my $all_docs = $db->run_command([
"distinct" => "$readPDFCollection",
"key"      => "assetUrl",
"query"    => {"pageURL"},
]);

Many thanks in advance for your help,

Gavin Colborne

Gerhard Gonter

unread,
Sep 27, 2017, 6:44:55 AM9/27/17
to mongod...@googlegroups.com
On Wed, Sep 27, 2017 at 7:13 AM, Gavin Colborne
<ga...@littleforest.co.uk> wrote:

> I am a little stuck with the "query" syntax on the run_command.
> ...

The query parameter is optional, but if it is present, it is a query
to match documents from the collection, e.g.:

# return list of ownerId strings who own Book objects
my $res1= $db->run_command ([ distinct => 'foxml.data', key =>
'ownerId', query => { model => 'Book' } ]);

# return list of ownerId strings who own anything
my $res2= $db->run_command ([ distinct => 'foxml.data', key => 'ownerId' ]);

I hope this helps.

regards, GG

Gavin Colborne

unread,
Oct 18, 2017, 12:59:58 AM10/18/17
to mongodb-user
Dear Gerhard,

Thank you for your example, I actually needed to get a value with the distinct result set and I am not sure it is possible!
I ended up using a Perl hash to get the unique rows in the collection (which is what I was trying to do with distinct).

Appreciate your help though,

All the best,

Gavin
Reply all
Reply to author
Forward
0 new messages