Apsaradb for MongoDB 2.4 and later versions are more user-friendly for space query.$ GeoWithin, translated from: http://docs.mongodb.org/manual/reference/operator/geoWithin/#op._S_geoWithin
$ Geowithin
Use in mongodb2.4$ GeoWithinThe operator (or function, also the same as below) replaces the discarded$Operator.$ GeoWithinAn operator is another specific point, line, or other geometric element that supports querying within a geometric element (completely within this geometric element.$ GeoWithinThe operator supports geojson as the query condition.$ GeoWithinThe operator does not return the result set of sorting. The query efficiency is higher than that supported by sorting.$ NearOr$ NearSphereFast operators.
$ Geowithin supports spatial indexes. Unlike version 2.2.3 $ geowithin, it requires a spatial index to improve the space query efficiency.
The syntax for querying all elements inside a polygon is as follows:
DB. <collection>. Find ({<location field>:
{$ Geowithin:
{$ Geometry:
{Type: "Polygon ",
Coordinates: [[[<lng1>, <lat1>], [<lng2>, <lat2>]...]
}}}})
Note that the sequence of coordinates must be as follows: "longitude, dimension ".
The following example shows the geographical name elements of all indexes within a polygon range.
db.places.find( { loc : { $geoWithin : { $geometry : { type : "Polygon" , coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ] } } } } )
For geometric queries, see:
- $ Box
- $ Polygon
- $ Center(Define a circle)
- $ CenterSphere(Defines the circle on an elliptical sphere)