Mongoose Schema for geoJson coordinates

前端 未结 3 2181
清歌不尽
清歌不尽 2021-02-09 00:43

I tried to create a schema for geojson but have had some problems with syntax for coordinates.

Here\'s my current code:

var DataSchema = new Schema({
           


        
相关标签:
3条回答
  • 2021-02-09 01:13

    Like this;

    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'}
      }
    });
    

    Here is your update route handler, it converts coordinates string to number array;

    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');
    });
    
    0 讨论(0)
  • 2021-02-09 01:17

    A GeoJSON field has to be included a geometry type as a string. So a GeoJSON field must be defined like the following;

    geometry: { type: { type: String }, coordinates: [Number] }
    

    or if you want to define a default value you might use the below line;

    geometry: { type: { type: String, default:'Point' }, coordinates: [Number] }
    

    Good luck..

    0 讨论(0)
  • 2021-02-09 01:26

    try this:

    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}
      }
    });
    
    0 讨论(0)
提交回复
热议问题