📜  MongoDB地理空间命令

📅  最后修改于: 2020-11-23 01:00:27             🧑  作者: Mango

MongoDB地理空间命令

地理空间命令仅包含一个命令,即geoSearch。它用于执行利用MongoDB的干草堆索引功能的地理空间查询。

干草堆索引:通过创建按第二个条件分组的对象桶来增加搜索量。

MongoDB geoSearch命令

geoSerach命令加载了可以由MongoDB的干草堆索引功能使用的接口。根据一些不同的查询(例如干草堆)收集结果后,它用于返回基于位置的结果。

句法:

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

它接受包含以下字段的文档:

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:  }

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.

让我们以集合位置为例:

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

上面的命令返回一种类型的书店,所有文件距收集位置中距坐标[-73.96466,40.78546]的最大距离为8个单位,最多返回50个结果。

覆盖默认读取问题

我们可以使用“读取关注”选项覆盖默认的读取关注级别。例如:

db.runCommand(
   {
      geoSearch: "places",
      near: [ -73.9667, 40.78 ],
      search : { type : "book store" },
      readConcern: { level: "majority" }
    }
)