我正在开发一个小型的NodeJS Web应用程序,使用Mongoose来访问我的MongoDB数据库。下面是我的集合的简化模式:
var MySchema = mongoose.Schema({
content: { type: String },
location: {
lat: { type: Number },
lng: { type: Number },
},
modifierValue: { type: Number }
});
很遗憾,我无法按照更方便我的方式对从服务器检索到的数据进行排序。我希望能根据它们与给定位置(位置)的距离来排序我的结果,但同时考虑一个修饰函数和一个modifierValue作为输入。
我打算做的事情写在下面。然而,这种排序功能似乎不存在。
MySchema.find({})
.sort( modifierFunction(location,this.location,this.modifierValue) )
.limit(20) // I only want the 20 "closest" documents
.exec(callback)
modifierFunction返回一个Double类型的值。
到目前为止,我已经研究过使用mongoose的$near函数的可能性了,但是它似乎不能排序,也不允许使用修改器函数。
由于我对node.js和mongoose还比较新,所以我可能在解决问题时采取了完全错误的方法,因此我很乐意重新设计我的编程逻辑。
提前感谢您的帮助。