创建和运行 Atlas Search 查询
Atlas Search queries take the form of an aggregation pipeline stage. Atlas 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. To learn more about these pipeline stages, see Choose the Aggregation Pipeline Stage.
Atlas Search also provides query operators and collectors that you can use inside the $search
and $searchMeta
aggregation pipeline stages. The Atlas Search operators allow you to locate and retrieve relevant data from the collection on your Atlas cluster. The collector returns a document representing the search metadata results.
You can use Atlas Search operators to query terms, phrases, geographic shapes and points, numeric values, similar documents, synonymous terms, and more. You can also search using regex and wildcard expressions. The Atlas 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 Atlas 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.
要了解有关 Atlas Search 操作符的事务语法、选项和用法的详情,请参阅在 Atlas Search 查询中使用操作符和收集器。
mongod
andmongot
on the Same NodeWhen you run a query, Atlas 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 ormongos
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 acrossmongod
instances and eachmongot
knows about the data on themongod
on the same node only. Therefore, you can't run Atlas 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, Atlas 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.Atlas Search performs the search and scoring and returns the document IDs and other search metadata for the matching results to
mongod
. Themongod
then performs a full document lookup implicitly for the matching results and returns the results to the client. If you use the$search
concurrent option in your query, Atlas Search enables intra-query parallelism. To learn more, see Parallelize Query Execution Across Segments.mongod
andmongot
on Different NodesWhen you run a query, the query first goes to the
mongod
based on the configured read preference. Themongod
process routes the search query through a load balancer on the same node, which distributes the requests across all of themongot
processes.The Atlas Search
mongot
process performs the search and scoring and returns the document IDs and metadata for the matching results tomongod
. Themongod
then performs a full document lookup for the matching results and returns the results to the client. If you use the$search
concurrent option in your query, Atlas Search enables intra-query parallelism. To learn more, see Parallelize Query Execution Across Segments.
Atlas Search 会将基于相关性的分数与结果集中的每个文档相关联。基于相关性的评分允许 Atlas Search 按从最高分到最低分的顺序返回文档。如果查询词频繁出现在某一文档中,Atlas Search 则会将此文档评为较高分;如果该查询词出现在集合内的很多文档中,则其评分较低。Atlas Search 还支持通过提升、衰减或其他修改选项来自定义基于相关性的默认分数。要了解有关自定义所生成分数的更多信息,请参阅对结果中的文档进行评分。
提示
另请参见:通过观看了解
支持的客户端
You can create and run Atlas Search queries using the following:
MongoDB Compass
Atlas CLI
后续步骤
To learn how to create and run a query, see Create a Query. For hands-on experience creating Atlas Search indexes and running Atlas Search queries against the sample datasets, try the tutorials in the following pages: