geoJson坐标的Mongoose模式

Mongoose Schema for geoJson coordinates

本文关键字:模式 Mongoose 坐标 geoJson      更新时间:2024-05-31

我试图为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}
  }
});