Store Polygon in MongoDB: strange validation error

1,347 views
Skip to first unread message

Daniele Margutti

unread,
Apr 20, 2015, 5:32:51 PM4/20/15
to mongod...@googlegroups.com
Hi guys,
I'm writing here because I've made tons of attempts in order to solve this problem without success.
My app has an alarm schema defined as:

var alarmSchema = new mongoose.Schema({
//...other keys
geo : {
'type': { type: String, required: true, enum: ["Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon"] },
coordinates : {type: [] , index: '2dsphere'}
}
});

Now I would to store a polygon. I'm using this example code:

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?

Will Berkeley

unread,
Apr 21, 2015, 10:26:27 AM4/21/15
to mongod...@googlegroups.com
What version of MongoDB are you using? Works fine for me on 2.6.6. Is this the exact code? Maybe the value of list isn't what you think it is in the real code?

-Will

Ильшат Мукминов

unread,
Jun 21, 2015, 5:29:08 AM6/21/15
to mongod...@googlegroups.com
Hello!
I have the same error. MongoDB version is 3.0.3. C#-code is:

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




вторник, 21 апреля 2015 г., 19:26:27 UTC+5 пользователь Will Berkeley написал:

David Jhanyan

unread,
Mar 11, 2016, 11:14:54 AM3/11/16
to mongodb-user
I am using MultiPoint instead of Polygon, but have the same error. Mongodb version 3.0.10, index is "2dsphereIndexVersion": 2.

Wan Bachtiar

unread,
Mar 13, 2016, 8:08:27 PM3/13/16
to mongodb-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:

  • Example documents.
  • The error message that you are getting.
  • The method on how to generate the error message.
  • If you use any, MongoDB driver version.

Kind regards,

Wan

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages