使用geojson和mongoose将多边形坐标插入Mongo DB时出错

时间:2018-10-05 07:45:13

标签: node.js mongodb mongoose

我是Nodejs和Mongo DB的初学者。我正在尝试将区域创建为方形多边形。我正在使用Nodejs来存储在数据库中。

我的猫鼬纲要是

select * from phone_numbers 
where id in (
    select id from
    phone_numbers
    group by id
    having count(*)=1
              )

我正在尝试使用以下内容插入坐标

select * from phone_numbers t1
  where exists ( select 1 from phone_numbers t2
                  where t1.id=t2.id
                  group by t2.id
                  having count(*)=1
                 )

我的坐标数组包含

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var momentTimezone  = require('moment-timezone');
var moment          = require('moment');


const geofencingAreasSchema = new mongoose.Schema({
    name: String,
    coordinates: [],    
    created_at: {
        type: Date,
        default: new moment.tz ('Asia/Dubai').format('Y-M-D HH:mm:ss')
   }
});
 geofencingAreasSchema.index({ coordinates:"2d" });  
/**
 * Export Schema
 */
module.exports = mongoose.model('geofencing_areas', geofencingAreasSchema,'geofencing_areas');

运行后出现类似错误

  

{MongoError:无法提取地理位置键:{_id:   ObjectId('5bb6ff2fddff79200cd5d5ef'       ),created_at:新日期(1538714135000),名称:“ 11”,坐标:[[[55.187775       33242337,25.35359425987688],[55.18777533242337,55.28710840825352],[55.28       710840825352,25.26372890279708],[25.26372890279708,25.35359425987688],[5       5.18777533242337,25.35359425987688]]],__v:0}点只能包含数字元素

1 个答案:

答案 0 :(得分:0)

我找到了解决办法

模式

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var momentTimezone  = require('moment-timezone');
var moment          = require('moment');


const geofencingAreasSchema = new mongoose.Schema({
    name: String,
    loc: {
      type: {type: String},
      coordinates: []
    },
    created_at: {
        type: Date,
        default: new moment.tz ('Asia/Dubai').format('Y-M-D HH:mm:ss')
   }
});     
 geofencingAreasSchema.index({loc: '2dsphere'});
/**
 * Export Schema
 */
module.exports = mongoose.model('geofencing_areas', geofencingAreasSchema,'geofencing_areas');

插入功能

const Zone = new GeofencingModel({
   name:row.toString()+col.toString(),
   loc:{type:'Polygon',coordinates : [coordinates]},
   created_at: new moment.tz('Asia/Dubai').format('Y-M-D HH:mm:ss')
 });
 Zone.save((err, zones) => {
   if (err) {
      return false;
   } else {
     return respose;
   }
});