var alarmSchema = new mongoose.Schema({
//...other keys
geo : {
'type': { type: String, required: true, enum: ["Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon"] },
coordinates : {type: [] , index: '2dsphere'}
}
});
var alarmObj = new Alarm();
//... set other properties
var list = [[[10.371094,42.391009],[14.238281,44.024422],[16.259766,42.130821],[14.370117,40.010787],[12.700195,41.804078],[10.371094,42.391009]]];
var geoobj = {"type":"Polygon","coordinates":list};
newAlarmObj.geo = geoobj;
As you can see polygon definition should be conform to GeoJSON Polygon schema (array of array with coordinates, where the first array is the polygon itself; moreover polygon is closed and last coordinate pair is the same of the first one).
However when I try to save it into my MongoDB this is the error:
{"message":"Error: MongoError: Can't extract geo keys: { _id: ObjectId('553568b8491c81ab3db4ce0b'), flynnRegion: null, minMagnitude: 2, enableHourEnd: null, enableHourStart: null, enabled: true, type: 2, user: ObjectId('553535d3e60543e43be44f07'), name: "MIo Allarme", geo: { type: "Polygon", coordinates: [ [ [ 10.371094, 42.391009 ], [ 14.238281, 44.024422 ], [ 16.259766, 42.130821 ], [ 14.370117, 40.010787 ], [ 12.700195, 41.804078 ], [ 10.371094, 42.391009 ] ] ] }, __v: 0 } Point must only contain numeric elements"}
... "Point must only contain numeric elements"...What? This is not a point, this is a polygon (in fact if I try to insert [lon,lat] array it works).Where am I wrong?Any idea? public class TestEntity {
[BsonId] public ObjectId Id;
public GeoJsonPolygonCoordinates<GeoJson2DGeographicCoordinates> Polygon { get; set; }
}
// Creating index: if (!col.IndexExists("Polygon"))
{ col.CreateIndex(IndexKeys.GeoSpatialSpherical("Polygon"), IndexOptions.SetName("Polygon")); }
}
...
// Save some polygon item.Polygon = new GeoJsonPolygonCoordinates<GeoJson2DGeographicCoordinates>( new GeoJsonLinearRingCoordinates<GeoJson2DGeographicCoordinates>( new List<GeoJson2DGeographicCoordinates> { new GeoJson2DGeographicCoordinates(-10, 0), new GeoJson2DGeographicCoordinates(-10, 10), new GeoJson2DGeographicCoordinates(0, 10), new GeoJson2DGeographicCoordinates(0, 0), new GeoJson2DGeographicCoordinates(-10, 0) })); _col.Save(_user);
Hi David,
Although you may share a similar error message, it may be a different issue that is causing it.
Could you please start a new discussion with relevant details of:
Kind regards,
Wan