Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store geospatial information in mongoDB

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?

like image 883
José Luis Avatar asked Mar 07 '13 15:03

José Luis


People also ask

Does MongoDB support geospatial index?

MongoDB supports multiple GeoJSON types to store geospatial data. Throughout our examples, we'll mainly use the Point and Polygon types.

Does MongoDB support GeoJSON?

MongoDB supports the GeoJSON object types listed on this page. a field named coordinates that specifies the object's coordinates.

Which chart is good for geospatial information?

Choropleth. Choropleth charts use predefined shapes for geographical areas, e.g. the countries of the world, or the states of the United States. They are useful for comparing aggregated values across defined geographical areas.


2 Answers

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

like image 75
Kay Avatar answered Sep 28 '22 02:09

Kay


2D Geospatial Index, Store Location Data

"All documents must store location data in the same order. If you use latitude and longitude as your coordinate system, always store longitude first. MongoDB’s 2d spherical index operators only recognize [ longitude, latitude] ordering."

Here's a good post about Geospatial Queries in MongoDB in case you need it.

like image 40
Andy Refuerzo Avatar answered Sep 28 '22 01:09

Andy Refuerzo