Focus on IT Recommend

Home > node.js - Using geoJSON in mongoose schema and use it in a query

node.js - Using geoJSON in mongoose schema and use it in a query



I'm new to mongoDB and the usage of geoJSON within it.

I have the following schema I created using the mongoose module:

var mongoose    = require('mongoose');

var hikeSchema = mongoose.Schema({

  name: String,
  area: String,
  length: Number,
  time: String,
  Difficulty: { type : Number, min : 0 , max :5 },
  start_coord: {
    lon: Number,
    lat: Number
  rank_count: Number,
  avg_overall_rating: { type : Number, min : 0 , max :5 },
  completed_count: Number,
  description: String,
  ranking_pages: [


module.exports = mongoose.model('Hike', hikeSchema);

I used a simple Number type to represent to longitude and latitude inside a sub object within the schema.

I don't know if this is the way to go about it. I want it to be saved as a geoJSON object and then run queries on this object.

node.js mongodb mongoose geojson
  this question
asked Aug 29 '14 at 11:53 David Tzoor 150 5 18


2 Answers

I recommand you to see the mongoDB part for geolocation datas :

To store longitude, latitude, MongoDB take you the correct data structure to save items :

To store location data as legacy coordinate pairs, use an array or an embedded document. When possible, use the array format:

loc : [ < longitude > , < latitude > ]

Consider the embedded document form:

loc : { lng : , lat : }

Arrays are preferred as certain languages do not guarantee associative map ordering.

Recommend:node.js - mongoose geojson in schema, "Can't extract geo keys" error

/lon coordinates. old version: var schema = mongoose.Schema({ id: String, name: String, address: String, city: String, zip: String, country: String, phoneNumber: String, mobile: String, website: String,

For all points, if you use longitude and latitude, store coordinates in longitude, latitude order.

On your shema, change

start_coord: {
  lon: Number,
  lat: Number

By :

start_coord: {
  lng: Number,
  lat: Number

Then you should add a 2dindex for your field start_coords to use all mongoDB geospatial queries :

db.collection.ensureIndex( { start_coord : "2d" } , { min : <l ower bound > , max : < upper bound > } )

EDIT It's important to set min and max if your longitude and latitude is not a real lng/lat (for example if you could use 2dindexes in a orthonormal map)

Now you can use all geospatial query operators :

  this answer
answered Aug 29 '14 at 12:27 throrin19 7,570 4 17 38      That's good for legacy pairs but you can also use GeoJSON. GeoJSON has better support for spherical computations. If your hikes are short enough, it won't matter much if you use 2d or 2dsphere computations. –  wdberkeley Aug 29 '14 at 15:53


You can also do it this way:

start_coord: {
    type: [Number],   // [<longitude>, <latitude>]
    index: '2d'   // create the geospatial index

Check this tutorial: How to use geospatial indexing in mongoDb

  this answer
answered Jun 3 '16 at 1:07 Kegham K. 316 3 16


Recommend:GeoJSON working with Mongoose/Mongodb in Node.js

record is blank. I assume this is due to a mis-configuration in the schema. Below is one record from the Mongodb command line. I removed a couple elements for privacy purposes. It looks as expected: { "_id" : ObjectId("56c77259336aa

------splitte line----------------------------