geoSearch

On this page本页内容

geoSearch

Deprecation

MongoDB 4.4 deprecates the geoHaystack index and the geoSearch command. MongoDB 4.4不支持geoHaystack索引和geoSearch命令。Use a 2d index with $geoNear or $geoWithin instead.使用带有$geoNear$geoWithin二维索引

The geoSearch command provides an interface to MongoDB’s haystack index functionality. geoSearch命令为MongoDB的haystack索引功能提供了一个接口。These indexes are useful 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 that contains the following fields.geoSearch命令接受包含以下字段的文档

Field字段Type类型Description描述
geoSearch string The collection to query.要查询的集合。
search document Query to filter documents.查询以筛选文档。
near array Coordinates of a point.点的坐标。
maxDistance number Optional.可选。Maximum distance from the specified point.距指定点的最大距离。
limit number Optional.可选。Maximum number of documents to return.要返回的最大文档数。
readConcern document

Optional.可选。Specifies the read concern.指定读取关注点

Starting in MongoDB 3.6, the readConcern option has the following syntax: 从MongoDB 3.6开始,readConcern选项具有以下语法:readConcern: { level: <value> }

Possible read concern levels are:可能的阅读关注级别包括:

For more formation on the read concern levels, see Read Concern Levels.有关阅读关注级别的更多信息,请参阅阅读关注级别

For more information on the read concern levels, see Read Concern Levels.有关阅读关注级别的更多信息,请参阅阅读关注级别

comment any

Optional.可选。A user-provided comment to attach to this command. 用户提供了要附加到此命令的注释。Once set, this comment appears alongside records of this command in the following locations:设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).

New in version 4.4.版本4.4中的新功能。

Behavior行为

Limit限度

Unless specified otherwise, the geoSearch command limits results to 50 documents.除非另有规定,否则geoSearch命令将结果限制为50个文档。

Sharded Clusters碎片簇

geoSearch is not supported for sharded clusters.分片群集不支持geoSearch

Transactions事务

geoSearch can be used inside multi-document transactions.geoSearch可用于多文档事务

Important

In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. 在大多数情况下,与单文档写入相比,多文档事务会带来更大的性能成本,而多文档事务的可用性不应取代有效的模式设计。For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.也就是说,对于许多场景,适当地建模数据将最大限度地减少对多文档事务的需求。

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

Examples示例

Consider the following example:考虑下面的例子:

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

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

Note

  • To use read concern level of "majority", replica sets must use WiredTiger storage engine.

    You can disable read concern "majority" for a deployment with a three-member primary-secondary-arbiter (PSA) architecture; however, this has implications for change streams (in MongoDB 4.0 and earlier 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 a node may not reflect the most recent version of the data in the system.
db.runCommand(
   {
      geoSearch: "places",
      near: [ -73.9667, 40.78 ],
      search : { type : "restaurant" },
      readConcern: { level: "majority" }
    }
)

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.要确保单个线程可以读取自己的写入,请对副本集的主线程使用"majority"读取关注点和"majority"写入关注点。