我正在尝试根据保存的纬度和经度信息的值在mongoDB数据库中查找一些记录。我想在我的集合上应用下面的函数和find:
exports.findDistance = findDistance(latitude, longitude) {
console.log('calculating with lat = ' + latitude + ' and longitude = ' + longitude);
var radian = 0.0174532925;
var x = Math.pow(Math.sin((latitude - this.latitude) * radian / 2), 2);
var y = Math.cos(latitude - radian) * Math.cos(this.latitude - radian);
var z = Math.pow(Math.sin((longitude - this.longitude) * radian/2), 2);
var result = 2*3956*Math.asin(Math.sqrt(x + y * z));
return result < 10;
}当我的计算结果小于10时,此函数返回true (我使用数据库中的纬度和经度信息,由' This‘对象和两个参数引用)。如何从mongoDB获取适用于此函数的所有消息?
我尝试过以下几种方法:
exports.index = function(req, res) {
var latitude = value1;
var longitude = value2;
Message.find(Message.findDistance, [], function(err, msgs) {
// render the page
});
}但是,mongoose find函数似乎并没有调用我的findDistance函数(在我的开发人员控制台中看不到输出,我只是从我的mongoDB集合中获得所有返回的结果)。另外,如何将纬度和经度参数传递给findDistance函数?
我的消息架构如下所示:
Message = new Schema({
text: { type: String, required: true },
parent: String,
user: ObjectId,
latitude: Number,
longitude: Number,
date: { type: Date, default: Date.now }
});有人能帮帮我吗?:)
发布于 2011-05-03 13:44:07
您的findDistance函数需要在MongoDB中定义,而不是在node.js中定义,以便它在运行时可以访问数据库字段。幸运的是,MongoDB将JavaScript用于其存储过程,因此该函数应该可以工作。
有关存储和使用MongoDB存储过程的说明,请参阅here。
发布于 2013-01-10 15:29:21
它等同于存储过程,但不是相同的。建议避免使用MongoDB存储函数。
有关更多信息,请访问:http://docs.mongodb.org/manual/applications/server-side-javascript/
https://stackoverflow.com/questions/5862058
复制相似问题