Josh Kim
Josh Kim

Reputation: 225

How can I ensure decimal precision in mongodb geospatial coordinates?

I need to save longitude/latitude coordinates in mongodb and I'm not sure how to store them. No examples I see online show conversion to string or splitting up the decimal parts into 2 ints. I'm assuming that in order to enable geo indexing, I'd have to store them as doubles, so for example:

{ _id : 100, pos: { long : 126.9, lat : 35.2 }, type : "restaurant"}

I need at least 6 decimal places of precision, so how does this work in mongodb?

Upvotes: 4

Views: 3848

Answers (2)

mjhm
mjhm

Reputation: 16705

MongoDB's geospatial index uses a conventional geohash. You can configure the precision of the hash to be up to 32bits, which gives you the equivalent of about 8 or 9 decimal places of precision. So you'll lose some precision from doubles, but still plenty for your application.

Upvotes: 5

madhead
madhead

Reputation: 33422

MongoDB supports double datatype. It is used for all floating point numbers. As MongoDB implements BSON spec, we may think of it's double as of BSON double. Which is 8 bytes (64-bit IEEE 754 floating point) according to the specification. And it supports 15-17 significant digits.

So do not worry.

Upvotes: 7

Related Questions