MongoDB 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.

Syntax:

It accepts the document that contains the following fields:

FieldTypeDescription
geoSearchstringIt is the name of the collection on which you want to perform geoSearch.
searchdocumentIt is the query that is used to filter the document.
neararrayIt is the coordinates of the point where we want to perform geosearch.
maxDistancenumberWe can define the maximum distance to where we want to perform the search.
LimitnumberWe can limit the maximum number document it will return.
readConcerndocumentWe 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.

Example

Let's take the example of collection location:

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:






Latest Courses