javascript - geoJson 坐标的 Mongoose 模式

标签 javascript node.js mongodb express mongoose

我尝试为 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] }

祝你好运……

关于javascript - geoJson 坐标的 Mongoose 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28749471/

相关文章:

javascript - 在移动设备上隐藏 div 中的项目

javascript - 从 ASP.NET 背后的代码调用 Javascript 函数

node.js - 无法运行 npm install

mongodb - 查询 MongoDB GridFS?

mongodb - 如何对另一个集合中的键进行 mapreduce

javascript - 如果文本溢出 HTML/CSS,如何使 div 滚动? (没有固定高度)?

javascript - 检查服务器端更新的侵入性最小的方法是什么

node.js - Socket.io 无法验证第一个证书

javascript - 在 Node.js/Express.js 中,如何将 JSON 对象从服务器传输到客户端?

正则表达式 : capturing group?