$geometry
The $geometry operator in Amazon DocumentDB is used to specify a GeoJSON geometry object as part of a geospatial query. This operator is used in conjunction with other geospatial query operators like $geoWithin and $geoIntersects to perform spatial queries on your data.
In Amazon DocumentDB, the $geometry operator supports the following GeoJSON geometry types:
-
Point
-
LineString
-
Polygon
-
MultiPoint
-
MultiLineString
-
MultiPolygon
-
GeometryCollection
Parameters
-
type: The type of the GeoJSON geometry object, e.g.,Point,Polygon, etc. -
coordinates: An array of coordinates representing the geometry. The structure of the coordinates array depends on the geometry type.
Example (MongoDB Shell)
The following example demonstrates how to use the $geometry operator to perform a $geoIntersects query in Amazon DocumentDB.
Create sample documents
db.locations.insertMany([ { "_id": 1, "name": "Location 1", "location": { "type": "Point", "coordinates": [-73.983253, 40.753941] } }, { "_id": 2, "name": "Location 2", "location": { "type": "Polygon", "coordinates": [[ [-73.998427, 40.730309], [-73.954348, 40.730309], [-73.954348, 40.780816], [-73.998427, 40.780816], [-73.998427, 40.730309] ]] } } ]);
Query example
db.locations.find({ "location": { "$geoIntersects": { "$geometry": { "type": "Polygon", "coordinates": [[ [-73.998, 40.730], [-73.954, 40.730], [-73.954, 40.781], [-73.998, 40.781], [-73.998, 40.730] ]] } } } })
Output
[
{
"_id": 2,
"name": "Location 2",
"location": {
"type": "Polygon",
"coordinates": [
[
[-73.998427, 40.730309],
[-73.954348, 40.730309],
[-73.954348, 40.780816],
[-73.998427, 40.780816],
[-73.998427, 40.730309]
]
]
}
}
]
Code examples
To view a code example for using the $geometry command, choose the tab for the language that you want to use: