-1, "ua" => 1};my $resultSet = $collection ->find({moderated => 1, markers => {'$all'=>$obj->{markers}}}) ->sort($sort)" name="description" /> -1, "ua" => 1};my $resultSet = $collection ->find({moderated => 1, markers => {'$all'=>$obj->{markers}}}) ->sort($sort)" />

How can I sort by multiple fields in mongodb with Perl?


How can I get multiple sort in MongoDB with Perl?

My current approach looks something like this:

my $sort = {"is_instock" => -1, "ua" => 1}; my $resultSet = $collection ->find({moderated => 1, markers => {'$all'=>$obj->{markers}}}) ->sort($sort) ->limit(25); @{$result} = $resultSet->all;

But, i got array sorted by one field(ua). What i did wrong?


The basic problem here is that a "hash" in Perl is ordered by "key" by default. In order to get the "order of insertion" you need to use <a href="https://metacpan.org/pod/Tie::IxHash" rel="nofollow">Tie::IxHash</a> as follows:

use Tie::IxHash; my %sort; tie ( %sort, 'Tie::IxHash' ); my $sort = \%sort; $sort = { "is_instock" => -1, "ua" => 1 };

Then when you use this in your MongoDB query, the keys are considered in the order you inserted them, rather than their lexcial order.

It should have been orderd that way anyhow since the keys are in lexical order, but I suggest you did something wrong and you need to be aware of the insertion order anyway.

The otherwise reason is that "in_stock" does not exist, or is not the true path name to the field. You need to specifiy the full path to the field with <a href="http://docs.mongodb.org/manual/core/document/#dot-notation" rel="nofollow">"dot notation"</a> otherwise the path is invalid.


  • Email SAS html output
  • C#: Decoding JPEG images with 12-bit precision using Silverlight FJCore library?
  • How to prevent stack overflow when using Scala's Predef.implicitly
  • OpenMP for dependent variables
  • Rails 4 order by virtual attribute
  • What to use (best/good practice) for the secret key in HMAC solution?
  • How to search for a record and then delete it
  • iterparse fails to parse a field, while other similar ones are fine
  • bcrypt-nodejs compare method returns false every time
  • R Leaflet Legend: specify order instead of alphabetical
  • Python find continuous interesctions of intervals
  • Remove previous Directions Route Google Maps
  • How to turn (A, B, C) into (AB, AC, BC) with Pig?
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • Using a canvas object in a thread to do simple animations - Java
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • Sort List of Strings By Version
  • How to suppress a dialog
  • Rails Find when some params will be blank
  • Linq Objects Group By & Sum
  • Using $this when not in object context
  • Retrieving value from sql ExecuteScalar()
  • Date difference with leap year
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • How to limit post in wp_query
  • ORA-29908: missing primary invocation for ancillary operator
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • php design question - will a Helper help here?
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Benchmarking RAM performance - UWP and C#
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Checking variable from a different class in C#
  • Binding checkboxes to object values in AngularJs
  • Converting MP3 duration time