对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

查询参考

MongoDB Search queries take the form of an aggregation pipeline stage. MongoDB Search provides $search and $searchMeta stages, both of which must be the first stage in any query pipeline, including the $lookup and $unionWith sub-pipelines. These stages can be used in conjunction with other aggregation pipeline stages in your query pipeline.

根据所选的管道阶段,查询返回全文搜索的搜索结果或关于搜索结果的元数据:

聚合管道阶段
用途

返回全文搜索的搜索结果。

返回关于搜索结果的元数据。

MongoDB Search 还提供可在 $search$searchMeta 聚合管道阶段中使用的查询运算符和收集器。MongoDB Search 操作符允许您从集群上的集合中查找和检索相关数据。收集器返回表示搜索元数据结果的文档。

您可以使用MongoDB Search 操作符来查询词、短语、地理形状和点、数值、类似文档、同义词等。

You can also search using regex and wildcard expressions. The MongoDB Search compound operator allows you to combine multiple operators inside your $search stage to perform a complex search and filter of data based on what must, must not, or should be present in the documents returned by MongoDB Search. You can use the compound operator to also match or filter documents in the $search stage itself. Running $match after $search is less performant than running $search with the compound operator.

如需了解有关操作符和收集器的更多信息,请参阅操作符和收集器。

mongodmongot 在同一节点上

When you run a query, MongoDB Search uses the configured read preference to identify the node on which to run the query. The query first goes to the MongoDB process, which is mongod for a replica set cluster or mongos for a sharded cluster.

For a replica set cluster, the MongoDB process routes the query to the mongot on the same node. For sharded clusters, your cluster data is partitioned across mongod instances and each mongot knows about the data on the mongod on the same node only. Therefore, you can't run MongoDB Search queries that target a particular shard. mongos directs the queries to all shards, making these scatter gather queries. If you use zones to distribute a sharded collection over a subset of the shards in the cluster, MongoDB Search routes the query to the zone that contains the shards for the collection that you are querying and runs your $search queries on just the shards where the collection is located.

MongoDB Search 执行搜索和评分,并将匹配结果的文档ID 和其他搜索元数据返回 mongod。然后,mongod 对匹配结果隐式执行完整文档查找,并将结果返回客户端。

如果在查询中使用 $search 并发选项,则 MongoDB Search 将启用查询内并行机制。要了解更多信息,请参阅并行查询各分段。

mongodmongot 在不同节点上

When you run a query, the query first goes to the mongod based on the configured read preference. By default, the mongod process routes the search query through a load balancer on the same node, which distributes the requests across all of the mongot processes. Alternatively, if you set the searchNodePreference parameter for your $search stage, MongoDB Search runs your query against the same search node each time.

MongoDB Search mongot进程执行搜索和评分,并将匹配结果的文档ID 和元数据返回给 mongod。然后,mongod 对匹配结果执行完整文档查找,并将结果返回给客户端。如果在查询中使用 $search 并发 选项, MongoDB Search 将启用查询内并行机制。要学习;了解更多信息,请参阅跨分段并行查询执行。

MongoDB Search 将基于相关性的分数与结果设立的每个文档相关联。基于相关性的评分允许MongoDB Search 按从最高分到最低分的顺序返回文档。如果查询术语频繁出现在文档中, MongoDB搜索对文档的评分较高;如果查询术语出现在集合中的许多文档中,MongoDB 搜索的评分较低。MongoDB Search 还支持通过提升、衰减或其他修改选项来自定义基于相关性的默认分数。要学习;了解有关自定义结果分数的更多信息,请参阅对结果中的文档进行评分。

提示

See: Learn by Watching

观看此视频,了解使用MongoDB Search 搜索和跟踪查询的概述。在本视频中,您可以学习;了解有关MongoDB Search 操作符 以及MongoDB Search 如何对结果中的文档进行评分的更多信息。

时长:15 分钟

您可以使用以下客户端创建并运行MongoDB搜索查询:

mongot 不返回任何错误,但在以下情况下返回空结果集: $search查询:

  • 引用了不存在的索引。如果您不在查询中按名称指定索引, MongoDB Search 默认使用名为 default 的索引。如果您没有名为 default 的索引,或者您指定的索引不存在,则MongoDB Search 不会返回错误并返回空结果设立。您可以使用 index 选项按名称指定有效索引。

  • 指定非索引字段。如果您对未编制索引的字段运行查询, MongoDB Search 不会返回错误并返回空结果设立。您必须仅指定索引字段作为 path 参数的值。您可以在集合的索引定义中启用动态映射,以确保自动为集合中的所有可动态索引的字段编制索引。要学习;了解更多信息,请参阅动态映射。

  • 在未索引为 string 类型的字段路径(Field Path)上使用 text操作符。如果字段被索引为 string 以外的MongoDB Search字段类型(例如 stringFacetautocomplete),则MongoDB Search 不会返回错误并返回空结果设立。您必须使用 string BSON 数据类型值作为 字符串 类型对字段索引,才能使用 文本 操作符查询字段。

mongot 如果$search查询出现以下情况,则会返回PlanExecutor错误:

  • 指定作为不正确数据类型进行索引的字段。在这种情况下,如果您运行查询, MongoDB Search 将返回一条错误消息,标识索引不正确的字段及其正确的数据类型。示例:

    PlanExecutor error during aggregation :: caused by :: Cannot facet on field "genres" because
    it was not indexed as a "stringFacet" field.

    示例,要对 stringnumberdate 字段运行facet(MongoDB搜索操作符)查询,请使用相应的MongoDB搜索字段类型为字段创建索引,例如 stringFacetnumberdate。要学习;了解详情,请参阅支持和不支持的数据类型。