根据条件查找 mongodb(在 mongoose+node.JS 中)记录
我试图根据保存的纬度和经度信息的值在我的 mongoDB 数据库中查找一些记录。我想在我的集合上应用以下函数和查找:
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
});
}
但是,我的 findDistance 函数似乎没有被 mongoose find 函数调用(在我的开发人员控制台中没有看到输出,我只是得到从我的 mongoDB 集合返回的所有结果)。另外,如何将纬度和经度参数传递给 findDistance 函数?
我的消息架构如下所示:
Message = new Schema({
text: { type: String, required: true },
parent: String,
user: ObjectId,
latitude: Number,
longitude: Number,
date: { type: Date, default: Date.now }
});
有人可以帮助我吗? :)
I'm trying to find some records in my mongoDB database based on the values of saved latitude and longitude information. I want to apply following function with a find on my collection:
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;
}
This function returns true when my calculated result is smaller than 10 (I use latitude and longitude information from my database, referenced by the 'this' object, and the two parameters). How can I get all messages from mongoDB that apply to this function?
I have tried the following:
exports.index = function(req, res) {
var latitude = value1;
var longitude = value2;
Message.find(Message.findDistance, [], function(err, msgs) {
// render the page
});
}
However, my findDistance function doesn't seem to be called by the mongoose find function (no output seen in my developer console and I just get all results returned from my mongoDB collection). Also, how can I pass the latitude and longitude parameters to the findDistance function?
My Message Schema looks as follows:
Message = new Schema({
text: { type: String, required: true },
parent: String,
user: ObjectId,
latitude: Number,
longitude: Number,
date: { type: Date, default: Date.now }
});
Can someone help me? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 findDistance 函数需要在 MongoDB 中定义,而不是在 node.js 中,以便它在运行时可以访问数据库字段。幸运的是,MongoDB 使用 JavaScript 作为其存储过程,因此该函数应该可以工作。
有关存储和存储的说明,请参阅此处使用 MongoDB 存储过程。
Your findDistance function needs to be defined in MongoDB, not in node.js, so that it has access to the database fields when it runs. Luckily, MongoDB uses JavaScript for its stored procedures, so the function should work.
See here for instructions on storing and using MongoDB stored procedures.
它相当于存储过程,但又不同。
并且建议避免使用MongoDB存储函数。
有关更多信息: http://docs.mongodb.org/manual/applications/服务器端 JavaScript/
Its equavilant to stored procedure but not same.
And it is recommended to avoid using MongoDB stored function.
For more Info: http://docs.mongodb.org/manual/applications/server-side-javascript/