无法使用多边形进行地理空间查询,使用mongoose进行点

时间:2017-03-24 03:26:12

标签: node.js mongodb mongoose geospatial

我正在尝试进行空间查询,在我的数据库中我有一个带有区域(多边形)的集合,我想找到涵盖这一点的区域。

以下是要测试的GIST:

https://gist.github.com/mariohmol/0cfebdcbdd885bf71e6f79e629f8eb63

代码的某些部分。

  • 架构:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert')

console.log('\n===========');
console.log('    mongoose version: %s', mongoose.version);
console.log('========\n\n');

var dbname = 'testing_geojsonPoint';
mongoose.connect('localhost', dbname);
mongoose.connection.on('error', function () {
  console.error('connection error', arguments);
});

var schema = new Schema({
  loc: {
      type: { type: String },
      coordinates: []
  },
  area: {
      type: { type: String, default: 'Polygon' },
      coordinates: []
  }
});
schema.index({ loc: '2dsphere' });
schema.index({ area: '2dsphere' });
var A = mongoose.model('A', schema);

我在mongoose版本上收到此错误:4.8.4:

//If I use this, works for the nearest

{ area: { $near: {
        type: 'Point', coordinates: [-43.941932916641235,-19.931718548878326] }}} 


//but if i do with geowithin it gives me the error:

{ area: {
   $geoWithin: { $geometry: {
     type: 'Point', coordinates: [-43.941932916641235,-19.931718548878326]
   }} 


Error: Invalid geoJSON type for $geoWithin "Point", must be "Polygon" or "MultiPolygon"
    at cast (/node_modules/mongoose/lib/cast.js:159:25)

0 个答案:

没有答案