How does one represent MongoDB GeoJSON fields in a Mongoose Schema? How does one represent MongoDB GeoJSON fields in a Mongoose Schema? mongoose mongoose

How does one represent MongoDB GeoJSON fields in a Mongoose Schema?


For reference, GeoJSON is officially supported in Mongoose 3.6

See the release notes here.

Example (from the docs):

new Schema({ loc: { type: [Number], index: '2dsphere'}})

... then ...

var geojsonPoly = { type: 'Polygon', coordinates: [[[-5,-5], ['-5',5], [5,5], [5,-5],[-5,'-5']]] }Model.find({ loc: { $within: { $geometry: geojsonPoly }}})// orModel.where('loc').within.geometry(geojsonPoly)


You must used Mixed to represent arrays of arrays. There is an open ticket to support this in the future.

@nevi_me is correct, you must declare the type property as he described.

Here's a gist: https://gist.github.com/aheckmann/5241574

See the mongoose tests here for more ideas: https://github.com/LearnBoost/mongoose/blob/master/test/model.querying.test.js#L1931


The mongoose-geojson-schema package was created to make it easy to have GeoJSON in Mongoose Schemas.