Last active
August 29, 2015 14:09
-
-
Save amarnus/43fc2896725e0c43d754 to your computer and use it in GitHub Desktop.
Maintaining Mongo sort order from Perl client.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use MongoDB; | |
use Tie::IxHash; | |
my $client = MongoDB::MongoClient->new; | |
my $database = $client->get_database( 'sorty' ); | |
my $collection = $database->get_collection( 'people' ); | |
my $sort = Tie::IxHash->new; | |
$sort->Push(age => -1); | |
#-- Do something else. | |
$sort->Push(name => -1); | |
my @documents = $collection->find({})->sort($sort)->all; | |
foreach my $document (@documents) { | |
print $document->{name} . "\t". $document->{age} . "\n"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Mango; | |
use Mango::BSON qw( bson_doc ); | |
my $mango = Mango->new; | |
my $database = $mango->db( 'sorty' ); | |
my $collection = $database->collection( 'people' ); | |
my $sort = bson_doc(age => -1, name => -1); | |
my $documents = $collection->find({})->sort($sort)->all; | |
foreach my $document (@$documents) { | |
print $document->{name} . "\t". $document->{age} . "\n"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use MongoDB; | |
use Tie::IxHash; | |
my $client = MongoDB::MongoClient->new; | |
my $database = $client->get_database( 'sorty' ); | |
my $collection = $database->get_collection( 'people' ); | |
my $sort = Tie::IxHash->new(age => -1, name => -1); | |
my @documents = $collection->find({})->sort($sort)->all; | |
foreach my $document (@documents) { | |
print $document->{name} . "\t". $document->{age} . "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment