我试图根据保存的经度和纬度信息的值在我的mongoDB数据库中找到一些记录。我要与我的集合查找适用以下功能:根据条件查找mongodb(在mongoose + node.JS中)记录
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;
}
此函数返回true,当我的计算结果小于10(我用的经度和纬度信息从我的数据库,由“此”对象引用,和这两个参数)。我如何从mongoDB获取适用于此函数的所有消息?
我曾尝试以下:
exports.index = function(req, res) {
var latitude = value1;
var longitude = value2;
Message.find(Message.findDistance, [], function(err, msgs) {
// render the page
});
}
但是,我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 }
});
有人能帮助我吗? :)