- Reference >
- Database Commands >
- Geospatial Commands >
- geoSearch
geoSearch¶
-
geoSearch¶ The
geoSearchcommand provides an interface to MongoDB’s haystack index functionality. These indexes are useful for returning results based on location coordinates after collecting results based on some other query (i.e. a “haystack.”)The
geoSearchcommand accepts a document that contains the following fields.Field Type Description geoSearchstring The collection to query. searchdocument Query to filter documents. neararray Coordinates of a point. maxDistancenumber Optional. Maximum distance from the specified point. limitnumber Optional. Maximum number of documents to return. readConcerndocument Optional. Specifies the read concern. The default level is
"local".To use a read concern level of
"majority", you must use the WiredTiger storage engine and start themongodinstances with the--enableMajorityReadConcerncommand line option (or thereplication.enableMajorityReadConcernsetting if using a configuration file).Only replica sets using
protocol version 1support"majority"read concern. Replica sets running protocol version 0 do not support"majority"read concern.To ensure that a single thread can read its own writes, use
"majority"read concern and"majority"write concern against the primary of the replica set.New in version 3.2.
Behavior¶
Unless specified otherwise, the geoSearch command
limits results to 50 documents.
Important
geoSearch is not supported for sharded
clusters.
Examples¶
Consider the following example:
The above command returns all documents with a type of
restaurant having a maximum distance of 6 units from the
coordinates [ -73.9667, 40.78 ] in the collection places up to a
maximum of 30 results.
Override Default Read Concern¶
To override the default read concern level of "local",
use the readConcern option.
The following operation on a replica set specifies a
Read Concern of "majority" to read the
most recent copy of the data confirmed as having been written to a
majority of the nodes.
Note
To use a read concern level of
"majority", you must use the WiredTiger storage engine and start themongodinstances with the--enableMajorityReadConcerncommand line option (or thereplication.enableMajorityReadConcernsetting if using a configuration file).Only replica sets using
protocol version 1support"majority"read concern. Replica sets running protocol version 0 do not support"majority"read concern.Regardless of the read concern level, the most recent data on a node may not reflect the most recent version of the data in the system.
To ensure that a single thread can read its own writes, use
"majority" read concern and "majority"
write concern against the primary of the replica set.