简体   繁体   English

如何在Mongoose中定义排序函数

[英]How to define a sort function in Mongoose

I'm developing a small NodeJS web app using Mongoose to access my MongoDB database. 我正在使用Mongoose开发一个小型NodeJS Web应用程序来访问我的MongoDB数据库。 A simplified schema of my collection is given below: 我的收藏的简化模式如下:

var MySchema = mongoose.Schema({                                 
    content:   { type: String },     
    location:  {                                                        
         lat:      { type: Number },                       
         lng:      { type: Number },                                              
    },
    modifierValue:  { type: Number }     
});

Unfortunately, I'm not able to sort the retrieved data from the server the way it is more convenient for me. 不幸的是,我无法以对我来说更方便的方式对服务器中检索到的数据进行排序。 I wish to sort my results according to their distance from a given position ( location ) but taking into account a modifier function with a modifierValue that is also considered as an input. 我想我的结果进行排序,根据从给定的位置( 位置 )的距离,但考虑到与也被认为是输入一个modifierValue的修饰功能。

What I intend to do is written below. 我打算做的事情如下。 However, this sort of sort functionality seems to not exist. 但是,这种排序功能似乎不存在。

MySchema.find({})
        .sort( modifierFunction(location,this.location,this.modifierValue) )
        .limit(20)       // I only want the 20 "closest" documents
        .exec(callback)

The mondifierFunction returns a Double. mondifierFunction返回一个Double。

So far, I've studied the possibility of using mongoose's $near function, but this doesn't seem to sort, not allow for a modifier function. 到目前为止,我已经研究了使用mongoose的$ near函数的可能性,但这似乎没有排序,不允许修饰函数。

Since I'm fairly new to node.js and mongoose, I may be taking a completely wrong approach to my problem, so I'm open to complete redesigns of my programming logic. 由于我对node.js和mongoose很新,我可能对我的问题采用了一种完全错误的方法,所以我愿意完全重新设计我的编程逻辑。

Thank you in advance, 先感谢您,

You might have found an answer to this already given the question date, but I'll answer anyway. 考虑到问题日期,您可能已经找到了答案,但无论如何我都会回答。

For more advanced sorting algorithms you can do the sorting in the exec callback. 对于更高级的排序算法,您可以在exec回调中进行排序。 For example 例如

MySchema.find({})
  .limit(20)
  .exec(function(err, instances) {
      let sorted = mySort(instances); // Sorting here

      // Boilerplate output that has nothing to do with the sorting.
      let response = { };

      if (err) {
          response = handleError(err);
      } else {
          response.status = HttpStatus.OK;
          response.message = sorted;
      }

      res.status(response.status).json(response.message);
  })

mySort() has the found array from the query execution as input and the sorted array as output. mySort()将查询执行中找到的数组作为输入,将排序后的数组作为输出。 It could for instance be something like this 例如,它可能是这样的

function mySort (array) {
  array.sort(function (a, b) {
    let distanceA = Math.sqrt(a.location.lat**2 + a.location.lng**2);
    let distanceB = Math.sqrt(b.location.lat**2 + b.location.lng**2);

    if (distanceA < distanceB) {
      return -1;
    } else if (distanceA > distanceB) {
      return 1;
    } else {
      return 0;
    }
  })

  return array;
}

This sorting algorithm is just an illustration of how sorting could be done. 这种排序算法只是如何进行排序的说明。 You would of course have to write the proper algorithm yourself. 你当然必须自己编写适当的算法。 Remember that the result of the query is an array that you can manipulate as you want. 请记住,查询的结果是一个可以根据需要操作的数组。 array.sort() is your friend. array.sort()是你的朋友。 You can information about it here . 您可以在此处获取相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM