我终于设法解决了这个问题。
本质上,此问题是由模式引起的,因为
2dIndex它被指向错误的字段
(type and coordinates)。
我使用以下 模式 解决了:
var mongoose = require('mongoose');var GeoJSON = require('geojson');var Schema = mongoose.Schema;var geoObjects = new Schema({ name : {type: String}, type: { type: String, enum: [ "Point", "LineString", "Polygon" ] }, coordinates: [Number], created_at: {type: Date, default: Date.now}, updated_at: {type: Date, default: Date.now}});// Sets the created_at parameter equal to the current timegeoObjects.pre('save', function(next){ now = new Date(); this.updated_at = now; if(!this.created_at) { this.created_at = now } next();});geoObjects.index({coordinates: '2dsphere'});module.exports = mongoose.model('geoObjects', geoObjects);和以下 查询 :
app.post('/query/', function(req, res) { // Grab all of the query parameters from the body. var lat = req.body.latitude; var long = req.body.longitude; var distance = req.body.distance; var query = GeoObjects.find({'type':'Point'}); // ...include filter by Max Distance if (distance) { // Using MongoDB's geospatial querying features. query = query.where('coordinates').near({ center: { type: 'Point', coordinates: [lat, long] }, // Converting meters to miles maxDistance: distance * 1609.34, spherical: true }); } // Execute Query and Return the Query Results query.exec(function(err, geoObjects) { if (err) res.send(err); // If no errors, respond with a JSON res.json(geoObjects); }); });希望对您有所帮助!
编辑
我提出的模式会给
LineStringsand 带来一些问题
Polygons。
这是允许使用的正确架构
geoQueries
linestring-model.js:
var mongoose = require('mongoose');var Schema = mongoose.Schema;// Creates a LineString Schema.var linestrings = new Schema({ name: {type: String, required : true}, geo : { type : {type: String, default: "LineString"}, coordinates : Array }, created_at: {type: Date, default: Date.now}, updated_at: {type: Date, default: Date.now}});// Sets the created_at parameter equal to the current timelinestrings.pre('save', function(next){ now = new Date(); this.updated_at = now; if(!this.created_at) { this.created_at = now } next();});linestrings.index({geo : '2dsphere'});module.exports = mongoose.model('linestrings', linestrings);多边形模型
var mongoose = require('mongoose');var Schema = mongoose.Schema;// Creates a Polygon Schema.var polygons = new Schema({ name: {type: String, required : true}, geo : { type : {type: String, default: "Polygon"}, coordinates : Array }, created_at: {type: Date, default: Date.now}, updated_at: {type: Date, default: Date.now}});// Sets the created_at parameter equal to the current timepolygons.pre('save', function(next){ now = new Date(); this.updated_at = now; if(!this.created_at) { this.created_at = now } next();});polygons.index({geo : '2dsphere'});module.exports = mongoose.model('polygons', polygons);LineString插入 :
{ "name" : "myLinestring", "geo" : { "type" : "LineString", "coordinates" : [ [ 17.811, 12.634 ], [ 12.039, 18.962 ], [ 15.039, 18.962 ], [ 29.039, 18.962 ] ] }}多边形插入:
{ "name" : "Poly", "geo" : { "type" : "Polygon", "coordinates" : [ [ [25.774, -80.190], [18.466, -66.118], [32.321, -64.757], [25.774, -80.190] ] ] }}


