2016-06-21 9 views
0

私はSOの他のQ/Aを見ても解決できなかった問題に直面しています。ラインストリングとポリゴンを保存できません - 平均+マングース

私はMEANスタックアプリケーションへのインタフェースとしてGoogle Maps APIのを使用するWebアプリケーションを構築しています。またMongooseを使用してMongoDB Schemaを作成しています。

Unluckily, I'm not able to store LineStrings and Polygons. I'm only able to store Points and query them as I want (e.g. find closest points to another one).

私はPOST私は次のエラーを取得するラインストリングまたはポリゴンにしようとすると:

geoObjects validation failed 
Cast to Array failed for value \"coordinates\" at path \"coordinates\" 

Here's a Gist with the full Postman Log

var mongoose = require('mongoose'); 
var GeoObjects = require('./model.js');  

app.post('/geoObjects', function(req, res) { 

     // Creates a new Point based on the Mongoose schema and the post body 
     var newObj = new GeoObjects(req.body); 

     // New Points is saved in the db. 
     newObj.save(function(err) { 
      if (err){ 
      res.send(err); 
      return; 
      } 

      // If no errors are found, it responds with a JSON of the new point 
      res.json(req.body); 
     }); 
    }); 

そして、これらは私がしようとしているのLineStringポリゴンの2つの例です:これは私のポストルート

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

var geoObjects = new Schema({ 
            name : {type: String}, 
            type: { 
               type: String, 
               enum: [ 
                 "Point", 
                 "LineString", 
                 "Polygon" 
                ], 
                default : 'Point' 
              }, 
            coordinates: [Number], 
}); 

geoObjects.index({coordinates: '2dsphere'}); 
module.exports = mongoose.model('geoObjects', geoObjects); 

です:

は、ここに私のスキーマですPOST:

{ 
    "name":"myPolygon", 
    "type": "Polygon", 
    "coordinates": [ 
        [ [25.774, -80.190], [18.466, -66.118], 
        [32.321, -64.757], [25.774, -80.190] 
        ] 
       ] 
} 

{ 
    "name":"myLineString",  
    "type": "LineString", 
    "coordinates": [ 
        [17.811, 12.634], [12.039, 18.962], 
        [15.039, 18.962], [27.039, 18.962] 
        ] 
} 

In a previous version, I had coordinates: [Schema.Types.Mixed] that allowed me to store all the 3 kind of geoObjects, but, unluckily, I was forced to switch to a different schema since with Schema.Types.Mixed it was not possible to let my queries work on points.

  1. なぜ私はラインストリングとポリゴンを投稿することはできませんよ?
  2. どうすればこの問題を解決できますか?

ご迷惑をおかけして申し訳ありません。コメントを残してください。

答えて

0

最初のスキーマを3つの異なるスキーマ(各タイプごとに1つ)で分割することで、この問題を解決することができました。

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

// Creates a LineString Schema. 
var linestrings = new Schema({ 
    name: {type: String, required : true}, 
    geo : { 
     type : {type: String, 
      default: "LineString"}, 
     coordinates : Array 
    }, 
    created_at: {type: Date, default: Date.now}, 
    updated_at: {type: Date, default: Date.now} 
}); 

// Sets the created_at parameter equal to the current time 
linestrings.pre('save', function(next){ 
    now = new Date(); 
    this.updated_at = now; 
    if(!this.created_at) { 
     this.created_at = now 
    } 
    next(); 
}); 

linestrings.index({geo : '2dsphere'}); 
module.exports = mongoose.model('linestrings', linestrings); 

ポリゴン:

ここgeoObjects

マーカーmodel.js

// Pulls Mongoose dependency for creating schemas 
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var markers = new Schema({ 
    name : {type: String}, 
    type: { 
     type: String, 
     default : 'Point' 
    }, 
    coordinates: {type: [Number], index:true}, 
    created_at: {type: Date, default: Date.now}, 
    updated_at: {type: Date, default: Date.now} 
}); 

// Sets the created_at parameter equal to the current time 
markers.pre('save', function(next){ 
    now = new Date(); 
    this.updated_at = now; 
    if(!this.created_at) { 
     this.created_at = now 
    } 
    next(); 
}); 

// Indexes this schema in 2dsphere format 
markers.index({coordinates: '2dsphere'}); 
module.exports = mongoose.model('markers', markers); 

折れ線-model.jsをgeoQueriesを使用して格納できるように正しいスキーマであります - モデル。JS

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

// Creates a Polygon Schema. 
var polygons = new Schema({ 
    name: {type: String, required : true}, 
    geo : { 
     type : {type: String, 
      default: "Polygon"}, 
     coordinates : Array 
    }, 
    created_at: {type: Date, default: Date.now}, 
    updated_at: {type: Date, default: Date.now} 
}); 

// Sets the created_at parameter equal to the current time 
polygons.pre('save', function(next){ 
    now = new Date(); 
    this.updated_at = now; 
    if(!this.created_at) { 
     this.created_at = now 
    } 
    next(); 
}); 

polygons.index({geo : '2dsphere'}); 
module.exports = mongoose.model('polygons', polygons); 

ラインストリングを挿入

{ 
    "name" : "myLinestring", 
    "geo" : { 
     "type" : "LineString", 
     "coordinates" : [ 
      [ 
       17.811, 
       12.634 
      ], 
      [ 
       12.039, 
       18.962 
      ], 
      [ 
       15.039, 
       18.962 
      ], 
      [ 
       29.039, 
       18.962 
      ] 
     ] 
    } 
} 

ポリゴンインサート:

{ 
    "name" : "Poly", 
    "geo" : { 
     "type" : "Polygon", 
     "coordinates" : [ 
          [ 
          [25.774, -80.190], [18.466, -66.118], 
          [32.321, -64.757], [25.774, -80.190] 
          ] 
         ] 
    } 
} 

私はそれが誰かを助けることを願っています。

関連する問題