Note that there are some explanatory texts on larger screens.

plurals
  1. POFind mongodb (in mongoose+node.JS) records based on a condition
    text
    copied!<p>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:</p> <pre><code>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 &lt; 10; } </code></pre> <p>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?</p> <p>I have tried the following:</p> <pre><code>exports.index = function(req, res) { var latitude = value1; var longitude = value2; Message.find(Message.findDistance, [], function(err, msgs) { // render the page }); } </code></pre> <p>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? </p> <p>My Message Schema looks as follows:</p> <pre><code>Message = new Schema({ text: { type: String, required: true }, parent: String, user: ObjectId, latitude: Number, longitude: Number, date: { type: Date, default: Date.now } }); </code></pre> <p>Can someone help me? :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload