Reputation: 65
I'm developing an API to store locations, including their coordinates, but I have no idea to model those. Should i go with
latitude: {
type: Float
},
longitude: {
type: Float
},
or is there a way of modeling them as an array, like [43.95 , -26.75]? I want later to filter them distance.
Upvotes: 2
Views: 3163
Reputation: 409
recently I worked on geo-location things, I think I can answer your question, its too late but may be this help others who is looking for solution.
location: {
type: {
type: String,
default: 'Point',
},
coordinates: [Number], // [22.2475, 14.2547] [longitude, latitude]
};
in above manner we have to introduce this attribute in our schema definition. Then we need to create a 2dsphere index in our collection like below: -
db.collection.createIndex( { location : "2dsphere" } )
that's it, now you are open to fire a query, based on location
db.collection.aggregate([
{
$geoNear: {
near: {
type: 'Point',
coordinates: [22.4568, 14.5412],
},
key: 'location',
distanceField: 'distance', // in meter
spherical: true,
},
}
])
Upvotes: 5
Reputation: 647
Best is to store them as GeoJSOn data , Then it is easy to query by apping a 2dsphere index.
One example of this type of storage is :-
"geolocation" : {
"type":"Point",
"coords":[-71.1410879999999960,42.4051060000000040]
}
The schema format :-
geolocation: {
type: { type: String },
coordinates: [Number],
}
Upvotes: 1
Reputation: 2597
In MongoDB, you can store geospatial data as GeoJSON objects or as legacy coordinate pairs.
Then you can use Geospatial Queries.
Upvotes: 2