0
我想基於一個函數進行排序。我目前正在做以下工作。使用貓鼬服務器端排序(mongodb + node.js)
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) {
...
});
現在我想基於一些函數,它在用戶輸入進行排序:
var sortFunc = function(x){ return (x - doc.score); };
// where doc.score is an attribute of the document I am searching on
// and x is a user provided value
,我無法找到一個方法來做到這一點。我試着按以下步驟EVAL此功能:
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);
});
,但我得到了以下錯誤:
err: Error: eval failed: db assertion failure
retval: null
任何幫助就這將是真棒。 非常感謝