versione Mongoose: 3.6 versione nodo: 0.10Usando find() con coordinate geospaziali in Mongoose (NodeJS + MongoDB)
Ho cercato di risolvere questo problema per ore. Voglio trovare tutti i documenti più vicini di maxDistance ad alcune coordinate. Sto provando ad usare le specifiche GeoJSON di MongoDB (2dsphere), in modo da poter inserire la distanza in metri.
Questo è il mio schema "venue.js":
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);
Questo è dove inserisco le ctrlVenue.js query:
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);
});
}
Quando eseguo il codice che ricevo l'errore:
"Error #101: CastError: Cast to number failed for value \"[object Object]\" at path \"geo\""
Se invece modifico questa linea:
$near: lonLat,
con
$near: geo,
ho correttamente ottenere i documenti, ma d'altra parte, non riesco a usare metri come unità di misura. Ho basato la mia ipotesi sulla tabella seguente: abbondanza http://docs.mongodb.org/manual/reference/operator/query-geospatial/
ho visto esempi di funzionamento utilizzando $ geometria ma nessuno con $ nei pressi. Che cosa sto facendo di sbagliato?
Hmm, la query è costruire correttamente, ma non sono sicuro perché mangusta sta gettando una misura! – Derick
@Derick è quello che pensavo. E questo tipo di uso mi sembra così basilare che sarei molto sorpreso se fossi l'unico ad attraversarlo ... – syymza