使用 Mongoose (mongodb + node.js) 进行服务器端排序

发布于 2024-12-16 17:45:04 字数 1122 浏览 2 评论 0原文

我正在尝试根据函数进行排序。我目前正在执行以下操作,并且有效。

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

对此的任何帮助都很棒。 多谢

I am trying to sort based on a 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 takes in a user input:

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

and I can't find a way to do this. I tried to eval 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.
Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小巷里的女流氓 2024-12-23 17:45:04

我认为你需要这样做:

var mongoose = require('mongoose');
mongoose.connect(uri);
mongoose.connection.on("open", function(err){
  mongoose.connection.db.eval("function(x){ return x; }", "asdf", function (err, retval) {
      console.log('err: '+err);
      console.log('retval: '+retval);
  });
});

这可以在我的电脑上运行。您必须确保连接可用。

I think you need do like this:

var mongoose = require('mongoose');
mongoose.connect(uri);
mongoose.connection.on("open", function(err){
  mongoose.connection.db.eval("function(x){ return x; }", "asdf", function (err, retval) {
      console.log('err: '+err);
      console.log('retval: '+retval);
  });
});

This can work on my PC. You must ensure that the connection is available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文