我正面临一个问题,我无法在SO上查看其他问答。
我正在构建一个使用 Google Maps Api 作为 MEAN Stack应用程序的界面的Web应用程序。我也使用 Mongoose 来创建 MongoDB架构。
不幸的是,我无法存储 LineStrings 和多边形。我只能存储积分并根据需要查询(例如找到距离另一个最近的点)。
当我尝试发布 LineString 或多边形时,我收到以下错误:
geoObjects validation failed
Cast to Array failed for value \"coordinates\" at path \"coordinates\"
Here's a Gist with the full Postman Log
这是架构:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var geoObjects = new Schema({
name : {type: String},
type: {
type: String,
enum: [
"Point",
"LineString",
"Polygon"
],
default : 'Point'
},
coordinates: [Number],
});
geoObjects.index({coordinates: '2dsphere'});
module.exports = mongoose.model('geoObjects', geoObjects);
这是我的邮政路线:
var mongoose = require('mongoose');
var GeoObjects = require('./model.js');
app.post('/geoObjects', function(req, res) {
// Creates a new Point based on the Mongoose schema and the post body
var newObj = new GeoObjects(req.body);
// New Points is saved in the db.
newObj.save(function(err) {
if (err){
res.send(err);
return;
}
// If no errors are found, it responds with a JSON of the new point
res.json(req.body);
});
});
这是我尝试发布的 LineString 和多边形的两个示例:
{
"name":"myPolygon",
"type": "Polygon",
"coordinates": [
[ [25.774, -80.190], [18.466, -66.118],
[32.321, -64.757], [25.774, -80.190]
]
]
}
{
"name":"myLineString",
"type": "LineString",
"coordinates": [
[17.811, 12.634], [12.039, 18.962],
[15.039, 18.962], [27.039, 18.962]
]
}
在以前的版本中,我有
coordinates: [Schema.Types.Mixed]
允许我存储所有3种geoObjects,但是,不幸的是,我 因此而被迫切换到不同的模式Schema.Types.Mixed
我无法让我的查询工作 分。
提前致谢,如果您需要澄清问题,请发表评论。
答案 0 :(得分:0)
我设法解决了这个问题,将初始模式拆分为3种不同的模式,每种类型一种。
以下是正确的模式,允许使用geoQueries
并存储geoObjects
<强>标记model.js 强>
// Pulls Mongoose dependency for creating schemas
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var markers = new Schema({
name : {type: String},
type: {
type: String,
default : 'Point'
},
coordinates: {type: [Number], index:true},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});
// Sets the created_at parameter equal to the current time
markers.pre('save', function(next){
now = new Date();
this.updated_at = now;
if(!this.created_at) {
this.created_at = now
}
next();
});
// Indexes this schema in 2dsphere format
markers.index({coordinates: '2dsphere'});
module.exports = mongoose.model('markers', markers);
<强>线串-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);
LineString Insert :
{
"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]
]
]
}
}
我希望它会帮助别人。