【发布时间】:2011-12-31 14:06:44
【问题描述】:
我正在尝试根据函数进行排序。我目前正在执行以下操作,并且有效。
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
我找不到办法做到这一点。我尝试按如下方式评估此功能:
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
对此的任何帮助都会很棒。 非常感谢
【问题讨论】:
标签: sorting mongodb collections server-side mongoose