Skip to content

Instantly share code, notes, and snippets.

@eastenluis
Created December 6, 2017 15:18
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save eastenluis/d4564daf7312c657748fc6a3dc5fceec to your computer and use it in GitHub Desktop.
Save eastenluis/d4564daf7312c657748fc6a3dc5fceec to your computer and use it in GitHub Desktop.
Mongoose GeoJSON schema example
const mapItemSchema = new mongoose.Schema({
name: String,
location: {
// It's important to define type within type field, because
// mongoose use "type" to identify field's object type.
type: {type: String, default: 'Point'},
// Default value is needed. Mongoose pass an empty array to
// array type by default, but it will fail MongoDB's pre-save
// validation.
coordinates: {type: [Number], default: [0, 0]}
}
});
const MapItem = mongoose.model('MapItem', mapItemSchema);
MapItem.create({
name: 'Toronto',
location: {
type: 'Point',
// Place longitude first, then latitude
coordinate: [-79.3968307, 43.6656976]
}
});
@sibelius
Copy link

sibelius commented Aug 4, 2018

having a default to 0 0 is not a good idea

@abhishek-mani
Copy link

having a default to 0 0 is not a good idea

why ?

@zubair1024
Copy link

I believe that's because it is a valid location, hence when if there is a missing value it will default to resolve to 0,0 coordinates.

@fdorantesm
Copy link

I believe that's because it is a valid location, hence when if there is a missing value it will default to resolve to 0,0 coordinates.

It's a non usable location. If you have Toronto POIS you could use Toronto Center instead, maybe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment