简体   繁体   English

根据条件查找mongodb(在mongoose+node.JS中)记录

[英]Find mongodb (in mongoose+node.JS) records based on a condition

I'm trying to find some records in my mongoDB database based on the values of saved latitude and longitude information.我试图根据保存的纬度和经度信息的值在我的 mongoDB 数据库中查找一些记录。 I want to apply following function with a find on my collection:我想申请以下 function 并在我的收藏中找到:

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).当我的计算结果小于 10 时,此 function 返回 true(我使用数据库中的纬度和经度信息,由 'this' object 和两个参数引用)。 How can I get all messages from mongoDB that apply to this function?如何从 mongoDB 获取适用于此 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). 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?另外,如何将纬度和经度参数传递给 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?有人能帮我吗? :) :)

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.您的 findDistance function 需要在 MongoDB 中定义,而不是在 node.js 中定义,以便它在运行时可以访问数据库字段。 Luckily, MongoDB uses JavaScript for its stored procedures, so the function should work.幸运的是,MongoDB 使用 JavaScript 作为其存储过程,因此 function 应该可以工作。

Seehere for instructions on storing and using MongoDB stored procedures.有关存储和使用 MongoDB 存储过程的说明,请参见此处

Its equavilant to stored procedure but not same.它等同于存储过程,但不一样。 And it is recommended to avoid using MongoDB stored function.并且建议避免使用 MongoDB 存储的 function。

For more Info: http://docs.mongodb.org/manual/applications/server-side-javascript/欲了解更多信息: http://docs.mongodb.org/manual/applications/server-side-javascript/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM