Ответ 1
Мне удалось решить эту проблему.
По сути, проблема была вызвана схемой, так как 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 time
geoObjects.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 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);
});
});
Я надеюсь, что это поможет кому-то!
ИЗМЕНИТЬ
Схема, на которую я положил, вызывает несколько проблем с LineStrings
и 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 time
linestrings.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);
Многоугольник-model.js
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 time
polygons.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);
Вставка строки в строку:
{
"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]
]
]
}
}