I am trying to sort based on function. I am currently doing the following and it works.
var _criteria = ... some search criteria var _pageNumber = ... the page num I want to see var _nPerPage = ... the number of documents per page var _sort = {}; _sort.name = ... the column name I am sorting on _sort.order = ... asc or desc sort Collection.find(_criteria) .skip((_pageNumber-1)*_nPerPage) .limit(_nPerPage) .sort(_sort.name,_sort.order) .execFind(function (err, docs) { ... });
Now, I would like to sort based on some function that accepts user input:
var sortFunc = function(x){ return (x - doc.score); };
and I canβt find a way to do this. I tried to evaluate this function as follows:
var mongoose = require('mongoose'); var mdb = mongoose.connect(uri); var myfunc = function(x){ return x; }; mdb.connection.db.eval( myfunc, "asdf", function (err, retval) { console.log('err: '+err); console.log('retval: '+retval); });
but I get the following error:
err: Error: eval failed: db assertion failure retval: null
Any help on this would be awesome. Thank you very much
source share