Javascript 根据条件查找mongodb(在mongoose+;node.JS中)记录
我试图根据保存的纬度和经度信息的值在mongoDB数据库中查找一些记录。我想在我的收藏中应用以下功能:Javascript 根据条件查找mongodb(在mongoose+;node.JS中)记录,javascript,mongodb,node.js,geolocation,mongoose,Javascript,Mongodb,Node.js,Geolocation,Mongoose,我试图根据保存的纬度和经度信息的值在mongoDB数据库中查找一些记录。我想在我的收藏中应用以下功能: exports.findDistance = findDistance(latitude, longitude) { console.log('calculating with lat = ' + latitude + ' and longitude = ' + longitude); var radian = 0.0174532925; var x = Math.po
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;
}
但是,我的finddInstance函数似乎没有被mongoose find函数调用(在我的开发人员控制台中没有看到输出,我只是从我的mongoDB集合中得到所有返回的结果)。另外,如何将纬度和经度参数传递给FindInstance函数
我的消息架构如下所示:
Message = new Schema({
text: { type: String, required: true },
parent: String,
user: ObjectId,
latitude: Number,
longitude: Number,
date: { type: Date, default: Date.now }
});
有人能帮我吗?:) 您的FindInstance函数需要在MongoDB中定义,而不是在node.js中定义,以便它在运行时可以访问数据库字段。幸运的是,MongoDB使用JavaScript作为其存储过程,因此该函数应该可以工作
有关存储和使用MongoDB存储过程的说明,请参阅。它与存储过程相同,但不同。 建议避免使用MongoDB存储函数 有关更多信息:
Message = new Schema({
text: { type: String, required: true },
parent: String,
user: ObjectId,
latitude: Number,
longitude: Number,
date: { type: Date, default: Date.now }
});