GeoSpatial Command is implemented through an interface named geoSearch
. It performs a geospatial query that uses MongoDB
’s haystack index functionality.
In the latest version, MongoDB 5.0, deprecated
geoSearch
command andgeoHaystack
index are no longer available. Use a 2d index with $geoNear instead, or use geospatial query operators.
Haystack index increases search by generating buckets of objects collected by a second criterion. The geoSearch
command is loaded with an interface used by MongoDB
’s haystack index functionality.
It gives location-based results after collecting results based on multiple fields in geoSearch documents, like coordinates and other queries (i.e., a haystack).
db.runCommand({
geoSearch : "ABC",
near: [x, y].
maxDistance: length,
search: {type : "name" },
readConcern: { level: "default"},
limit: no_of_documents})
geoSearch
documentHere listed some fields that a geoSearch document contains:
Attributes | Genre | Description Description |
Geosearch | string | Name of the collection on which the geoSearch is performed.
|
Search | document | Query which is used to filter the document. |
Near | array | It is the coordinates of a point where the geoSearch is performed. |
maxDistance | number | The maximum distance to where the search is performed from the Specified point. |
Limit | number | The highest number of documents to return. |
comment | any | User provided comment |
readConcern | document | Read concern can be done using this syntax: read Concern: {level: <value>} Possible levels of read-concern are:
|
readConcern
The readConcern
field default value can be override.
To override, you can use these possible levels of read-concern:
"local"
"shanpnshot"
"majority"
,"linearrizable"
, etc.This demo code contains an updated readConcern value to highest as below:
db.runCommand({
geoSearch: "places",
near: [ -73.9667, 40.78],
search: {type : "School" ),
readConcern: {level: "highest"}})
The following commands give all the documents with a type 'School'
having the highest distance of 10 units
from points [ -63.4809, 41.89765]
in the collection education
to show a maximum of 30 results
:
import mongoDB as db# MongoDB GeoSearch Commanddb.rumCommand({geoSearch: "education",near: [-63.4809, 41.89765],maxDistance: 10,search: {type:: "School" },limit: 30})