- Reference >
- Database Commands >
- Geospatial Commands >
- geoSearch
geoSearch¶
-
geoSearch
¶ The
geoSearch
command 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
geoSearch
command accepts a document that contains the following fields.Field Type Description geoSearch
string The collection to query. search
document Query to filter documents. near
array Coordinates of a point. maxDistance
number Optional. Maximum distance from the specified point. limit
number Optional. Maximum number of documents to return. readConcern
document Optional. Specifies the read concern.
The readConcern option has the following syntax:
Changed in version 3.6.
Possible read concern levels are:
"local"
. This is the default read concern level."available"
. This is the default for reads against secondaries when Read Operations and afterClusterTime and “level” are unspecified. The query returns the instance’s most recent data."majority"
. Available for replica sets that use WiredTiger storage engine."linearizable"
. Available for read operations on theprimary
only.
For more formation on the read concern levels, see Read Concern Levels.
For
"local"
(default) or"majority"
read concern level, you can specify theafterClusterTime
option to have the read operation return data that meets the level requirement and the specified after cluster time requirement. For more information, see Read Operations and afterClusterTime.For more information on the read concern levels, see Read Concern Levels.
Behavior¶
Transactions¶
geoSearch
supports multi-document transactions.
Important
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transaction should not be a replacement for effective schema design. For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions. For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.
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 read concern level of
"majority"
, replica sets must use WiredTiger storage engine.For MongoDB 4.0.3+ (and 3.6.1+), you can disable read concern
"majority"
. For more information, see Disable Read Concern Majority.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.