Overview概述
In this guide, you can learn how to use the Node.js driver to run MongoDB Search queries on a collection. MongoDB Search enables you to perform full-text searches on collections hosted on MongoDB Atlas. MongoDB Search indexes specify the behavior of the search and which fields to index.在本指南中,您可以学习如何使用Node.js驱动程序在集合上运行MongoDB搜索查询。MongoDB搜索使您能够对MongoDB Atlas上托管的集合执行全文搜索。MongoDB搜索索引指定搜索行为以及要索引的字段。
Sample Data样本数据
The example in this guide uses the 本指南中的示例使用Atlas示例数据集中movies collection in the sample_mflix database from the Atlas sample datasets. sample_mflix数据库中的movies集合。To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the MongoDB Get Started guide.要了解如何创建免费的MongoDB Atlas集群并加载示例数据集,请参阅MongoDB入门指南。
Run a MongoDB Search Query运行MongoDB搜索查询
This section shows how to create an aggregation pipeline to run a MongoDB Search query on a collection. In your array of pipeline stages, add the 本节将展示如何创建聚合管道,以便在集合上运行MongoDB搜索查询。在管道级数组中,添加$search stage to specify the search criteria. Then, call the aggregate() method and pass your pipeline array as a parameter.$search级以指定搜索条件。然后,调用aggregate()方法并将管道数组作为参数传递。
Tip
To learn more about aggregation operations, see the Aggregation Operations guide.要了解有关聚合操作的更多信息,请参阅《聚合操作》指南。
Before running a MongoDB Search query, you must create a MongoDB Search index on your collection. 在运行MongoDB搜索查询之前,您必须在集合上创建MongoDB搜索索引。To learn how to programmatically create a MongoDB Search index, see the MongoDB Search and MongoDB Vector Search Indexes section of the Indexes guide.要了解如何以编程方式创建MongoDB搜索索引,请参阅索引指南的MongoDB搜索和MongoDB向量搜索索引部分。
MongoDB Search ExampleMongoDB搜索示例
This example runs a MongoDB Search query by performing the following actions:此示例通过执行以下操作运行MongoDB搜索查询:
Creates a创建一个$searchstage that instructs the driver to query for documents in which thetitlefield contains the word"Alabama"$search阶段,指示驾驶员查询title字段包含单词"Alabama"的文档Creates a创建一个$projectstage that instructs the driver to include thetitlefield in the query results$project阶段,指示驱动程序在查询结果中包含title字段Passes the pipeline stages to the将管道阶段传递给aggregate()method and prints the resultsaggregate()方法并打印结果
const pipeline = [
{
$search: {
index: "default", // Replace with your search index name替换为您的搜索索引名称
text: {
query: "Alabama",
path: "title"
}
}
},
{
$project: {
title: 1
}
}
];
const cursor = collection.aggregate(pipeline);
for await (const document of cursor) {
console.log(document);
}
{
_id: new ObjectId('...'),
title: 'Alabama Moon'
}
{
_id: new ObjectId('...'),
title: 'Crazy in Alabama'
}
{
_id: new ObjectId('...'),
title: 'Sweet Home Alabama'
}
Tip
Node.js Driver MongoDB Search Examples驱动程序MongoDB搜索示例
To view more examples that use the Node.js driver to perform Atlas Search queries, see MongoDB Search Tutorials in the Atlas documentation.要查看更多使用Node.js驱动程序执行Atlas搜索查询的示例,请参阅Atlas文档中的MongoDB搜索教程。
Additional Information附加信息
To learn more about MongoDB Search, see MongoDB Search in the Atlas documentation.要了解有关MongoDB搜索的更多信息,请参阅Atlas文档中的MongoDB搜索。
API Documentation文档
To learn more about the 要了解有关aggregate() method, see the API documentation.aggregate()方法的更多信息,请参阅API文档。