geoJson 坐标的猫鼬模式
Mongoose Schema for geoJson coordinates
我试图为 geojson 创建一个架构,但在坐标语法方面遇到了一些问题。
这是我当前的代码:
var DataSchema = new Schema({
properties: {
title: { type: String, required: true },
description: { type: String, required: true },
date: { type:Date, default:Date.now }
},
geometry: {
coordinates: []
}
});
我尝试使用 []
(空数组),它创建了 ''
和 [Number,Number]
但它不起作用。
我的问题是:我必须如何构建我的模式才能得到结果
coordinates: [ 3.43434343, 5.543434343 ]
没有引号,这样可以吗?
快速路线
app.post('/mountain_rescue', function (req, res){
new rescueData({properties:{title: req.body.title, description: req.body.description},geometry:{
coordinates:req.body.coordinates}}).save(function (e, result) {
console.log(result);
});
res.redirect('/mountain_rescue');
});
查看
<div id="AddingPanel">
<form method="post" action="mountain_rescue" >
Title:<input type="text" name="title">
Description:<textarea type="text" name="description"></textarea>
Coordinates:<input type="text" name="coordinates">
<button type="submit">Add</button>
</form>
试试这个:
var DataSchema = new Schema({
properties: {
title: { type: String, required: true },
description: { type: String, required: true },
date: { type:Date, default:Date.now }
},
geometry: {
coordinates: {type: Array, required: true}
}
});
像这样;
var DataSchema = new Schema({
properties: {
title: { type: String, required: true },
description: { type: String, required: true },
date: { type:Date, default:Date.now }
},
geometry: {
coordinates: { type: [Number], index: '2dsphere'}
}
});
这是您的更新路线处理程序,它将坐标字符串转换为数字数组;
app.post('/mountain_rescue', function (req, res) {
new rescueData({
properties: {
title: req.body.title, description: req.body.description
},
geometry: {
coordinates:req.body.coordinates.split(',').map(Number)
}
}).save(function (e, result) {
console.log(result);
});
res.redirect('/mountain_rescue');
});
GeoJSON 字段必须作为字符串包含在几何类型中。所以 GeoJSON 字段必须像下面这样定义;
geometry: { type: { type: String }, coordinates: [Number] }
或者如果您想定义一个默认值,您可以使用下面的行;
geometry: { type: { type: String, default:'Point' }, coordinates: [Number] }
祝你好运..
我试图为 geojson 创建一个架构,但在坐标语法方面遇到了一些问题。
这是我当前的代码:
var DataSchema = new Schema({
properties: {
title: { type: String, required: true },
description: { type: String, required: true },
date: { type:Date, default:Date.now }
},
geometry: {
coordinates: []
}
});
我尝试使用 []
(空数组),它创建了 ''
和 [Number,Number]
但它不起作用。
我的问题是:我必须如何构建我的模式才能得到结果
coordinates: [ 3.43434343, 5.543434343 ]
没有引号,这样可以吗?
快速路线
app.post('/mountain_rescue', function (req, res){
new rescueData({properties:{title: req.body.title, description: req.body.description},geometry:{
coordinates:req.body.coordinates}}).save(function (e, result) {
console.log(result);
});
res.redirect('/mountain_rescue');
});
查看
<div id="AddingPanel">
<form method="post" action="mountain_rescue" >
Title:<input type="text" name="title">
Description:<textarea type="text" name="description"></textarea>
Coordinates:<input type="text" name="coordinates">
<button type="submit">Add</button>
</form>
试试这个:
var DataSchema = new Schema({
properties: {
title: { type: String, required: true },
description: { type: String, required: true },
date: { type:Date, default:Date.now }
},
geometry: {
coordinates: {type: Array, required: true}
}
});
像这样;
var DataSchema = new Schema({
properties: {
title: { type: String, required: true },
description: { type: String, required: true },
date: { type:Date, default:Date.now }
},
geometry: {
coordinates: { type: [Number], index: '2dsphere'}
}
});
这是您的更新路线处理程序,它将坐标字符串转换为数字数组;
app.post('/mountain_rescue', function (req, res) {
new rescueData({
properties: {
title: req.body.title, description: req.body.description
},
geometry: {
coordinates:req.body.coordinates.split(',').map(Number)
}
}).save(function (e, result) {
console.log(result);
});
res.redirect('/mountain_rescue');
});
GeoJSON 字段必须作为字符串包含在几何类型中。所以 GeoJSON 字段必须像下面这样定义;
geometry: { type: { type: String }, coordinates: [Number] }
或者如果您想定义一个默认值,您可以使用下面的行;
geometry: { type: { type: String, default:'Point' }, coordinates: [Number] }
祝你好运..