用于geoJson坐标的Mongoose Schema

IOR*_*R88 7 javascript mongoose mongodb node.js express

我试图为geojson创建一个模式,但是在坐标语法方面遇到了一些问题.

这是我目前的代码:

var DataSchema = new Schema({
  properties: {
    title:       { type: String, required: true },
    description: { type: String, required: true },
    date:        { type:Date, default:Date.now }
  },
  geometry: {
       coordinates: []
  }
});
Run Code Online (Sandbox Code Playgroud)

我尝试使用[](空数组),它创建''[Number,Number],但它不工作.

我的问题是:我如何构建我的模式,以便得到结果

coordinates: [ 3.43434343, 5.543434343 ]
Run Code Online (Sandbox Code Playgroud)

没有引号,这可能吗?

快车道

   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');
  });
Run Code Online (Sandbox Code Playgroud)

视图

<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>
Run Code Online (Sandbox Code Playgroud)

efk*_*kan 14

GeoJSON字段必须包含几何类型作为字符串.因此,必须如下定义GeoJSON字段;

geometry: { type: { type: String }, coordinates: [Number] }
Run Code Online (Sandbox Code Playgroud)

或者如果要定义默认值,可以使用以下行;

geometry: { type: { type: String, default:'Point' }, coordinates: [Number] }
Run Code Online (Sandbox Code Playgroud)

祝好运..


Saf*_*afi 7

像这样;

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'}
  }
});
Run Code Online (Sandbox Code Playgroud)

这是你的更新路由处理程序,它将坐标字符串转换为数字数组;

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');
});
Run Code Online (Sandbox Code Playgroud)

  • 如果您以后想要执行地理空间查询,则需要这样做.如果您不需要这样做,则不需要 (2认同)