$geoWithin
The $geoWithin operator in Amazon DocumentDB is used to find documents whose location data (represented as GeoJSON objects) are completely contained within a specified shape, such as a polygon or multipolygon. This is useful for querying for objects that are located within a specific geographic region.
Parameters
-
$geometry: A GeoJSON object that represents the shape to query against.
Example (MongoDB Shell)
The following example demonstrates how to use the $geoWithin operator to find all airports located within the state of New York.
Create sample documents
// Insert state document db.states.insert({ "name": "New York", "loc": { "type": "Polygon", "coordinates": [[ [-79.76278, 45.0], [-73.94, 45.0], [-73.94, 40.5], [-79.76278, 40.5], [-79.76278, 45.0] ]] } }); // Insert airport documents db.airports.insert([ { "name": "John F. Kennedy International Airport", "type": "airport", "code": "JFK", "loc": { "type": "Point", "coordinates": [-73.7781, 40.6413] } }, { "name": "LaGuardia Airport", "type": "airport", "code": "LGA", "loc": { "type": "Point", "coordinates": [-73.8772, 40.7769] } }, { "name": "Buffalo Niagara International Airport", "type": "airport", "code": "BUF", "loc": { "type": "Point", "coordinates": [-78.7322, 42.9403] } } ]);
Query example
var state = db.states.findOne({"name": "New York"}); db.airports.find({ "loc": { "$geoWithin": { "$geometry": state.loc } } }, { "name": 1, "type": 1, "code": 1, "_id": 0 });
Output
[
{
"name": "John F. Kennedy International Airport",
"type": "airport",
"code": "JFK"
},
{
"name": "LaGuardia Airport",
"type": "airport",
"code": "LGA"
}
]
Code examples
To view a code example for using the $geoWithin command, choose the tab for the language that you want to use: