geoSearch

  • geoSearch
  • The geoSearch command provides an interface toMongoDB’s haystack index functionality. These indexes areuseful 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 thatcontains the following fields.

FieldTypeDescriptiongeoSearchstringThe collection to query.searchdocumentQuery to filter documents.neararrayCoordinates of a point.maxDistancenumberOptional. Maximum distance from the specified point.limitnumberOptional. Maximum number of documents to return.readConcerndocumentOptional. Specifies the read concern.

Starting in MongoDB 3.6, the readConcern option has the followingsyntax: readConcern: { level: <value> }

Possible read concern levels are:

For more information on the read concern levels, seeRead Concern Levels.

Behavior

Limit

Unless specified otherwise, the geoSearch commandlimits results to 50 documents.

Sharded Clusters

geoSearch is not supported for sharded clusters.

Transactions

geoSearch can be used inside multi-document transactions.

Important

In most cases, multi-document transaction incurs a greaterperformance cost over single document writes, and theavailability of multi-document transactions should not be areplacement for effective schema design. For many scenarios, thedenormalized data model (embedded documents and arrays) will continue to be optimal for yourdata and use cases. That is, for many scenarios, modeling your dataappropriately will minimize the need for multi-documenttransactions.

For additional transactions usage considerations(such as runtime limit and oplog size limit), see alsoProduction Considerations.

Examples

Consider the following example:

  1. db.runCommand({
  2. geoSearch : "places",
  3. near: [ -73.9667, 40.78 ],
  4. maxDistance : 6,
  5. search : { type : "restaurant" },
  6. limit : 30
  7. })

The above command returns all documents with a type ofrestaurant having a maximum distance of 6 units from thecoordinates [ -73.9667, 40.78 ] in the collection places up to amaximum 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 aRead Concern of "majority" to read themost recent copy of the data confirmed as having been written to amajority of the nodes.

Note

You can disable read concern "majority" for a deploymentwith a three-member primary-secondary-arbiter (PSA) architecture;however, this has implications for change streams (in MongoDB 4.0 andearlier only) and transactions on sharded clusters. For more information,see Disable Read Concern Majority.

  • Regardless of the read concern level, the most recent data on anode may not reflect the most recent version of the data in the system.
  1. db.runCommand(
  2. {
  3. geoSearch: "places",
  4. near: [ -73.9667, 40.78 ],
  5. search : { type : "restaurant" },
  6. readConcern: { level: "majority" }
  7. }
  8. )

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.