Mongoose Schema for geoJson坐标
我试图为geojson创build一个模式,但坐标的语法有一些问题。
这是我现在的代码:
var DataSchema = new Schema({ properties: { title: { type: String, required: true }, description: { type: String, required: true }, date: { type:Date, default:Date.now } }, geometry: { coordinates: [] } });
我尝试使用[]
(空数组),它创build''
和[Number,Number]
但它不起作用。
我的问题是:我如何构build自己的模式,以便得到结果
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: [Number], index: '2dsphere'} } });
这里是你的更新路由处理程序,它将坐标string转换为数组数组;
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字段必须包含几何types作为string。 所以GeoJSON字段必须定义如下:
geometry: { type: { type: String }, coordinates: [Number] }
或者如果你想定义一个默认值,你可以使用下面的行;
geometry: { type: { type: String, default:'Point' }, coordinates: [Number] }
祝你好运..
尝试这个:
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} } });