0

We need it for the following use case:

var myPerceptualHash = '0239582305980';
db.coll1.find().sort({ 
    $by : function( a, b ) {
        var md = require('hamming-distance');
        return hm(myPerceptualHash, a.phash) < hm(myPerceptualHash, b.phash);
    }
}).limit(1)

This is even more special than just defining a sorting function that relates a with b, as It will require to use an "external" parameter to contrast to at the moment of the sorting.

Diego Pamio
  • 1,377
  • 13
  • 21
  • Possible duplicate of [Mongo complex sorting?](http://stackoverflow.com/questions/3212919/mongo-complex-sorting) – joao May 06 '16 at 13:42
  • What is the `$by`? I can't see it in the sort docs. – BanksySan May 06 '16 at 14:09
  • The $by is how I expect to write the sorting custom function (like the $where operator for queries). this is different thant the possible duplicate in terms that I need to pass an external reference to the function, I need not only to compare a with b but also with an extra "myPerceptualHash" variable. Otherwise I could just use precalculated fields. – Diego Pamio May 06 '16 at 15:57
  • Yes, and you are importing some custom library. That can't be done in the mongo side (on the other hand, you can do the sort yourself in the client). You either inject all the necessary code in the sort function or AFAIK it is not possible. – joao May 06 '16 at 17:52
  • But if I inject all the necessary code in the server I would be able to do it? – Diego Pamio May 06 '16 at 18:02

0 Answers0