geoJson坐标的Mongoose模式
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>
GeoJSON字段必须包含一个作为字符串的几何类型。因此,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: [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');
});
试试这个:
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}
}
});
相关文章:
- 如何缩短MongoDB ObjectId并在Mongoose模式中使用它
- Mongoose TypeError:实例化模式类型的对象时,对象不是函数
- 使用mongoose将数据推送到mongoDB模式中的数组中
- 为什么我的Mongoose DB模式之一可能是“;强制转换为未定义的“;当它的实例被放置在第二个模式中时
- 如何在javascript对象中设置属性的类型,就像mongoose模式设计一样
- Mongoose模式:强制创建一个对象数组
- MongoDB(Mongoose)使数组成为模式的一部分
- Node.js+Mongoose:与数据库逻辑分离的模型/模式
- geoJson坐标的Mongoose模式
- 如何在不选择模式配置参数的情况下,使用mongoose在MongoDB模式实例化中的关联数组/对象中执行foreach
- 引用Mongoose中的另一个模式
- 使用Node.js在现有Mongoose模式中插入对象ID's到数组中
- 如何使用严格:false模式的mongoose向mongoDB文档添加字段
- 根据json格式的'描述'动态定义Mongoose模式
- 如何在mongoose js中嵌套相同的模式
- Mongoose中的Meteor Simple模式验证器
- 创建一个独立的用户模式,并使用 Mongoose/MongoDB 将其嵌入到消息模式中
- 如何在Mongoose中将模式属性设置为SubDocument类型
- 为什么我的模式不能在mongoose数组中添加默认值?
- 不在Mongoose模式中的嵌套对象上的点符号