Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@amarnus
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amarnus/43fc2896725e0c43d754 to your computer and use it in GitHub Desktop.
Save amarnus/43fc2896725e0c43d754 to your computer and use it in GitHub Desktop.
Maintaining Mongo sort order from Perl client.
#!/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";
}
#!/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";
}
#!/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