2016-03-22 16 views
0

Bu hatayı, konuma dayalı bir modeli sorgulamaya çalışırken anladım.

cevap

0

Bazı yanıtları SO üzerinde birleştirdikten sonra, tam uygulama örneği.

Modeli

// model.js 
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var ModelSchema = new Schema({ 
    location: { 
    type: [Number], 
    index: '2dsphere' 
    } 
}); 

ModelSchema.index({ location: 1}); 

module.exports = mongoose.model('model', ModelSchema); 

Kontrolör

// controller.js 
Model = require('path/to/model') 
exports.getByLocation = function(req, res, next) { 
    var coords = [+req.query.lon, +req.query.lat]; 

    Model.where('location').near({ 
     center: { 
      type: 'Point', 
      coordinates: coords 
     } 
    }) 
    .then(function(docs) { 
     res.json(docs); 
    }) 
    .catch(next); 
} 

Umut bu yardımcı olur :)