How to store geospatial information in mongoDB

José Luis picture José Luis · Mar 7, 2013 · Viewed 39.8k times · Source

Acording to the mongoDB documentation (link) if you want to store geospatial information in a document field, you have two options, an array or an embedded document, and the order should be always longitude, latitude.

If I want to use an embedded document, how can I ensure the field's order?

Or must the fields in the embedded document have a specific name?

Answer

Kay picture Kay · Mar 7, 2013

with an embedded document, regardless of the name of the field in the embedded document, the first field should contain the longitude value and the second field should contain the latitude value. For example:

 db.zips2.insert( { _id: 1, city: "b", loc: { x: -73.974, y: 40.764 } } )
 db.zips2.insert( { _id: 2, city: "b", loc: { x: -73.981, y: 40.768 } } )

Here the x field would be the longitude; and the y field would be the latitude.

Regards