Geospatial Command

The Geospatial Command includes only one command, which is geoSearch. It is used to perform a geospatial query that utilizes MongoDB's haystack index functionality.

Haystack index: It increases searches by creating buckets of objects grouped by a second criterion.

MongoDB geoSearch command

The geoSerach command loaded with an interface that can be used by MongoDB's haystack index functionality. It is used to return a location-based results after collecting result based on some different queries such as haystack.


   geoSearch : "read",
   near: [ -73.9667, 40.78 ],
   maxDistance : 6,
   search : { type : "tutorial" },
   limit : 30

It accepts the document that contains the following fields:

Field Type Description
geoSearch string It is the name of the collection on which you want to perform geoSearch.
search document It is the query that is used to filter the document.
near array It is the coordinates of the point where we want to perform geosearch.
maxDistance number We can define the maximum distance to where we want to perform the search.
Limit number We can limit the maximum number document it will return.
readConcern document We can specify the read concern using the following syntax;
readConcern: { level: <value> }
Possible read concern levels are:
  • "local": It is the default read concern level for the read operation.
  • "available": It returns the most recent data.
  • "majority": It is used by replica sets that use wiredTiger storage engine.
  • "linearizable": It is used for reading operations.


Let's take the example of collection location:

db.runCommand( { geoSearch : "location", near: [ -73.96466, 40.78546 ], maxDistance : 8, search : { type : "book store" }, limit : 50 })

The above command returns all the documents with a type of book store having a maximum distance of 8 units from the coordinates [ -73.96466, 40.78546 ] in the collection location up to a maximum of 50 results.

Overriding Default Read Concern

We can use the read concern option to override the default read concern level. As for example:

      geoSearch: "places",
      near: [ -73.9667, 40.78 ],
      search : { type : "book store" },
      readConcern: { level: "majority" }
Related Tutorial
Follow Us #
Contents +