If I have a document in the form of:
{
pos: { lat: 0, lon: 30 }
}
in the collection users (please pretend these are real lat / lon :-) )
What is the correct way to get all values within a certain radius of say: 50 miles?
Explanation: MongoDB supports search by field, range queries, regular expression searches.
MongoDB provides the find() that is used to find multiple values or documents from the collection. The find() method returns a cursor of the result set and prints all the documents. To find the multiple values, we can use the aggregation operations that are provided by MongoDB itself.
Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.
It's a 3 step process.
2dsphere
.$geoWithin
and $centerSphere
.To perform geospatial queries, you need to change the document structure to match a GeoJSON Point. Which looks like this.
loc : {
type : "Point",
coordinates : [lng, lat]
}
Sample code for translating your collection to the Point format.
// sample setup code.
// use test;
// db.positions.drop();
// db.positions.insert({
// pos : {
// lat : 0,
// lon : 30
// }
// });
db.positions.find().forEach(function (doc) {
var point = {
_id : doc._id,
loc : {
type : "Point",
coordinates : [doc.pos.lon, doc.pos.lat]
}
};
db.positions.update(doc, point);
});
db.positions.find().pretty();
Afterwards, you can use $geoWithin
and $near
operators in your queries like the example below.
var createLandmarkDoc = function (name, lng, lat) {
return {
name : name,
loc : {
type : "Point",
coordinates : [lng, lat]
}
};
};
var addNewLandmark = function(name, lng, lat){
db.landmarks.insert(createLandmarkDoc(name, lng, lat));
};
db.landmarks.drop();
// Step 1: Add points.
addNewLandmark("Washington DC", 38.8993487, -77.0145665);
addNewLandmark("White House", 38.9024593, -77.0388266);
addNewLandmark("Library of Congress", 38.888684, -77.0047189);
addNewLandmark("Patuxent Research Refuge", 39.0391718, -76.8216182);
addNewLandmark("The Pentagon", 38.871857, -77.056267);
addNewLandmark("Massachusetts Institute of Technology", 42.360091, -71.09416);
// Step 2: Create index
db.landmarks.ensureIndex({
loc : "2dsphere"
});
var milesToRadian = function(miles){
var earthRadiusInMiles = 3959;
return miles / earthRadiusInMiles;
};
var landmark = db.landmarks.findOne({name: "Washington DC"});
var query = {
"loc" : {
$geoWithin : {
$centerSphere : [landmark.loc.coordinates, milesToRadian(5) ]
}
}
};
// Step 3: Query points.
db.landmarks.find(query).pretty();
{
"_id" : ObjectId("540e70c96033ed0d2d9694fa"),
"name" : "Washington DC",
"loc" : {
"type" : "Point",
"coordinates" : [
38.8993487,
-77.0145665
]
}
}
{
"_id" : ObjectId("540e70c96033ed0d2d9694fc"),
"name" : "Library of Congress",
"loc" : {
"type" : "Point",
"coordinates" : [
38.888684,
-77.0047189
]
}
}
{
"_id" : ObjectId("540e70c96033ed0d2d9694fb"),
"name" : "White House",
"loc" : {
"type" : "Point",
"coordinates" : [
38.9024593,
-77.0388266
]
}
}
{
"_id" : ObjectId("540e70c96033ed0d2d9694fe"),
"name" : "The Pentagon",
"loc" : {
"type" : "Point",
"coordinates" : [
38.871857,
-77.056267
]
}
}
More Info:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With