Docs 菜单
Docs 主页
/ /

运行MongoDB搜索查询

在本指南中,您可以学习;了解如何查询MongoDB Search索引,以及如何在C++驾驶员应用程序中使用高级全文搜索功能。您可以使用 $search聚合管道阶段查询搜索索引。

要了解有关 $search 管道阶段的更多信息,请参阅 MongoDB Server 手册中的 $search 指南。

注意

Atlas和 Community Edition 版本要求

$search 聚合管道操作符仅适用于在运行 MongoDB v4.2 或更高版本的MongoDB Atlas集群上托管的集合,或者运行 MongoDB v8.2 或更高版本的MongoDB Community Edition集群上托管的集合。集合必须包含在MongoDB 搜索索引中。要了解有关此操作符所需设置和功能的更多信息,请参阅MongoDB 搜索文档。

sample_mflix.movies本指南中的示例使用Atlas示例数据集中的 集合。要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅MongoDB入门指南。

在对Atlas集合执行搜索之前,必须先在该集合上创建MongoDB Search索引。 MongoDB搜索索引是一种以可搜索格式对数据进行分类的数据结构。要学习;了解如何创建MongoDB搜索索引,请参阅 MongoDB搜索索引指南。

要使用 $search聚合管道阶段,您必须指定MongoDB 搜索查询操作符来指示要运行的查询类型。您可以选择使用收集器来指定查询输出的值和范围。要查看MongoDB 搜索可用的所有操作符和收集器的表格,请参阅Atlas文档中的操作符和收集器页面。

以下示例使用 compound操作符将多个操作符组合成一个查询。要学习;了解更多信息,请参阅Atlas文档中的复合操作指南。

该查询具有以下搜索条件:

  • genres 字段不得包含 Comedy

  • title 字段必须包含字符串 New York

查询还包括以下阶段:

  • $limit,用于将输出限制为 10 个结果。

  • $project,排除 title 以外的所有字段,并添加一个名为 score 的字段。

#include <iostream>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/exception/exception.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/pipeline.hpp>
int main() {
const mongocxx::instance instance{};
try {
const mongocxx::client client{mongocxx::uri{"<connection-string>"}};
auto collection = client["sample_mflix"]["movies"];
mongocxx::pipeline pipeline{};
auto search_stage = bsoncxx::from_json(R"(
{
"$search": {
"index": "test_index",
"compound": {
"mustNot": [
{
"text": {
"query": [ "Comedy" ],
"path": "genres"
}
}
],
"must": [
{
"text": {
"query": [ "New York" ],
"path": "title"
}
}
]
}
}
}
)");
pipeline.append_stage(search_stage.view());
pipeline.limit(10);
pipeline.project(bsoncxx::from_json(R"(
{ "_id" : 0, "title" : 1, "score" : { "$meta" : "searchScore" } }
)"));
auto cursor = collection.aggregate(pipeline);
for (const auto& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
} catch (const mongocxx::exception& e) {
std::cerr << "MongoDB error: " << e.what() << std::endl;
return EXIT_FAILURE;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
{ "title" : "New York, New York", "score" : 6.7870168685913085938 }
{ "title" : "New York", "score" : 6.2591872215270996094 }
{ "title" : "New York Doll", "score" : 5.3819599151611328125 }
{ "title" : "Escape from New York", "score" : 4.7203946113586425781 }
{ "title" : "Autumn in New York", "score" : 4.7203946113586425781 }
{ "title" : "Gangs of New York", "score" : 4.7203946113586425781 }
{ "title" : "Sleepless in New York", "score" : 4.7203946113586425781 }
{ "title" : "Sherlock Holmes in New York", "score" : 4.2036685943603515625 }
{ "title" : "New York: A Documentary Film", "score" : 4.2036685943603515625 }
{ "title" : "An Englishman in New York", "score" : 4.2036685943603515625 }

要学习有关可用MongoDB Search 操作符的更多信息,请参阅MongoDB Atlas文档中的操作符和收集器指南。

有关 MongoDB 搜索 的更多信息以及查看更多查询示例,请参阅 MongoDB 搜索 文档。

如果您想对存储在Atlas中的数据执行向量搜索,则必须使用MongoDB向量搜索。要学习;了解有关MongoDB Vector Search 的更多信息,请参阅MongoDB Vector Search 文档。

后退

运行数据库命令

在此页面上