With the introduction of 2.3 >
MongoDB has become even more useful with location data handling and querying. MongoDB stores documents as BSON, so each document with have all the document fields, which obviously potentially leads to larger databases than our conventional RMDBS.
I used to store polylines and polygons as a series of indexed points, with an extra field representing the order of each line (I was doing this to ensure consistency as I use JavaScript, so points weren't always stored in their correct order). It was something like this:
polyline: {
[
point: [0,0],
order: 0
],
[
point: [0,1],
order: 1
]
}
Whereas now I use:
polyline: {
type: 'LineString',
coordinates: [
[0,0],
[1,0]
]
}
I've seen an improvement in the size of documents, as some polylines can have up to 500 points.
However, I'm wondering what the benefits of storing all my Point
data as GeoJSON
would be. I am discouraged by the increase in document size, as for example:
loc: [1,0]
is way better than
loc: {
type: 'Point',
coordinates: [0,1]
}
and would thus be easier to work with.
My question is:
Is it better/recommended to store points as GeoJSON
objects as opposed to a 2 point array?
What I have considered is the following:
- Size constraints: I could potentially have millions of documents with a location, which might impact the size of the collection, and potentially my pocket.
- Consistency: It would be better do deal with every set of coordinates in the
lng, lat
format as opposed to sticking tolat, lng
for points, and the former for all my other location features. - Convenience: If I grab a point, and use a
$geoWithin
or$geoIntersects
with it, I wouldn't need to convert it to GeoJSON first before using it as aquery
parameter.
What I am unsure of is:
- Whether support for
loc: [x,y]
will be dropped in the future on MongoDB - Any indexing benefits from
2dsphere
as opposed to2d
- Whether any planned
GeoJSON
additions to MongoDB might result in the need for the consistency mentioned above.
I'd rather move to GeoJSON
while my data is still manageable, than switch in future under a lot of strain.
May I please kindly ask for a thoroughly (even if slightly) thought out answer. I won't select a correct answer soon, so I can evaluate any responses.
I'm also not sure if SO is the right place to pose the question, so if DBA is a more appropriate place I'll move the question there. I chose SO because there's a lot of MongoDB related activity here.