Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing find() with geospatial coordinates in Mongoose (NodeJS+MongoDB)
    primarykey
    data
    text
    <p>Mongoose version: 3.6 Node version: 0.10</p> <p>I have been trying to solve this problem for hours. I want to find all the documents closer than maxDistance to some coordinates. I am trying to use the GeoJSON specifications of MongoDB (2dsphere), so that I can input the distance in meters. </p> <p>This is my schema "venue.js":</p> <pre><code>var db = require('mongoose'), Schema = db.Schema, ObjectId = Schema.ObjectId; var venueSchema = new Schema({ geo: { type: [Number], index: '2dsphere'}, city: String, name: String, address: String }); module.exports = db.model('Venue', venueSchema); </code></pre> <p>This is where I insert the query ctrlVenue.js:</p> <pre><code>var Venue = require('../models/venue.js'); VenueController = function(){}; /** GET venues list ordered by the distance from a "geo" parameter. Endpoint: /venues Params: - geo: center for the list of venues - longitude, latitude (default: 25.466667,65.016667 - Oulu); - maxDistance: maxímum distance from the center for the list of venues (default: 0.09) **/ exports.getVenues =function(req, res) { var maxDistance = typeof req.params.maxDistance !== 'undefined' ? req.params.maxDistance : 0.09; //TODO: validate var geo = typeof req.params.geo !== 'undefined' ? req.params.geo.split(',') : new Array(25.466667, 65.016667); //TODO: validate var lonLat = { $geometry : { type : "Point" , coordinates : geo } }; Venue.find({ geo: { $near: lonLat, $maxDistance: maxDistance }}).exec(function(err,venues){ if (err) res.send(500, 'Error #101: '+err); else res.send(venues); }); } </code></pre> <p>When I run the code I receive the error:</p> <blockquote> <p>"Error #101: CastError: Cast to number failed for value \"[object Object]\" at path \"geo\""</p> </blockquote> <p>If I instead modify this line:</p> <pre><code>$near: lonLat, </code></pre> <p>with</p> <pre><code>$near: geo, </code></pre> <p>I correctly get the documents, but then, I cannot use meters as unit of measure. I based my assumptions on the following table: <a href="http://docs.mongodb.org/manual/reference/operator/query-geospatial/">http://docs.mongodb.org/manual/reference/operator/query-geospatial/</a></p> <p>I have seen plenty of functioning examples using <em>$geometry</em> but none together with <em>$near</em>. What am I doing wrong? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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