Javascript geoJson坐标的Mongoose模式

Javascript geoJson坐标的Mongoose模式,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我试图为创建模式,但坐标的语法有一些问题 以下是我当前的代码: var DataSchema = new Schema({ properties: { title: { type: String, required: true }, description: { type: String, required: true }, date: { type:Date, default:Date.now } }, geometry: {

我试图为创建模式,但坐标的语法有一些问题

以下是我当前的代码:

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


标题:
说明:
协调:
添加
试试这个:

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}
  }
});
像这样,

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

GeoJSON字段必须作为字符串包含在几何体类型中。因此,GeoJSON字段的定义必须如下所示:

geometry: { type: { type: String }, coordinates: [Number] }
或者,如果要定义默认值,可以使用下面的行

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

祝你好运

我以前试过,在mognodb中,结果是这样的“坐标”:[“1.434343,4.21322”],这些引号作为geojson不可读,我检查了我以前的项目。但我很好奇,为什么如果它是一个数组,mongo会加引号?我试过了,但它只在我键入一个数字时起作用:3.4343,否则在键入两个3.43、4.343时,它是未定义的。请确保req.body.coordinates是两个数字的数组,在您的情况下不是。它只是一个逗号分隔的字符串,例如3.43,4.343No,当它到达服务器时仍然是一个字符串,请尝试以下操作:;req.body.coordinates.split(“,”).map(Number)将返回一个numbersNo数组,这只是为了确保字符串转换为Number,看起来,驱动程序正在以任何方式执行此操作,如果以后要执行地理空间查询,则需要此操作。如果您不需要这样做,则不需要。以“点”为例,但您可能希望处理各种类型的要素,使用不同类型的坐标(点的数组、线字符串的数组、多边形的数组、多行线的数组)仅适用于点,而不适用于任何GeoJSON字段
geometry: { type: { type: String, default:'Point' }, coordinates: [Number] }