Docs Home → Develop Applications → MongoDB Manual
geoSearch![](/docs/v5.3/assets/link.svg)
geoSearch
Important
Removed in MongoDB 5.0
MongoDB 5.0 removes the deprecated geoHaystack index and
geoSearch
command. Use a 2d index with$geoNear
or one of the supported geospatial query operators instead.Upgrading your MongoDB instance to 5.0 and setting featureCompatibilityVersion to
5.0
will delete any pre-existing geoHaystack indexes.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.FieldTypeDescriptiongeoSearch
stringThe collection to query.search
documentQuery to filter documents.near
arrayCoordinates of a point.maxDistance
numberOptional. Maximum distance from the specified point.limit
numberOptional. Maximum number of documents to return.readConcern
documentOptional. Specifies the read concern.
Starting in MongoDB 3.6, the readConcern option has the following syntax:
readConcern: { level: <value> }
Possible read concern levels are:
"local"
. This is the default read concern level for read operations against the primary and secondaries."available"
. Available for read operations against the primary and secondaries."available"
behaves the same as"local"
against the primary and non-sharded secondaries. 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 more information on the read concern levels, see Read Concern Levels.
comment
anyOptional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
mongod log messages, in the
attr.command.cursor.comment
field.Database profiler output, in the
command.comment
field.currentOp
output, in thecommand.comment
field.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
Behavior![](/docs/v5.3/assets/link.svg)
Limit![](/docs/v5.3/assets/link.svg)
Unless specified otherwise, the geoSearch
command
limits results to 50 documents.
Sharded Clusters![](/docs/v5.3/assets/link.svg)
geoSearch
is not supported for sharded clusters.
Transactions![](/docs/v5.3/assets/link.svg)
geoSearch
can be used inside 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 transactions 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![](/docs/v5.3/assets/link.svg)
Consider the following example:
db.runCommand({ geoSearch : "places", near: [ -73.9667, 40.78 ], maxDistance : 6, search : { type : "restaurant" }, limit : 30 })
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![](/docs/v5.3/assets/link.svg)
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
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.
db.runCommand( { geoSearch: "places", near: [ -73.9667, 40.78 ], search : { type : "restaurant" }, readConcern: { level: "majority" } } )
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.