Docs 菜单
Docs 主页
/ /

运行MongoDB搜索查询

In this guide, you can learn how to use the Kotlin Sync 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.

本指南中的示例使用Atlas示例数据集sample_mflix数据库中的 movies集合。要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。要学习;了解有关聚合操作和构建者的更多信息,请参阅 聚合操作指南。

This section shows how to create an aggregation pipeline to run a MongoDB Search query on a collection. You can use the Aggregates.search() builder method to create a $search pipeline stage, which specifies the search criteria. Then, call the aggregate() method and pass your pipeline as a parameter.

注意

仅适用于 Atlas for MongoDB v4.2 及更高版本

This aggregation pipeline operator is only available for collections hosted on MongoDB Atlas clusters running v4.2 or later that are covered by a MongoDB Search index. Learn more about the required setup and the functionality of this operator from the MongoDB Search documentation.

Before running a MongoDB Search query, you must create a MongoDB Search index on your collection. To learn how to programmatically create a MongoDB Search index, see the Create a Search Index section in the Indexes guide.

此示例通过执行以下操作来运行MongoDB搜索查询:

  • 使用 Aggregates.search() 构建器方法构建 $search 阶段,指示驱动程序查询 title 字段中包含单词 "Alabama" 的文档

  • 使用 Aggregates.project() 构建器方法构造 $project 阶段,指示驾驶员在查询结果中包含 title字段

  • 将管道阶段传递到 aggregate() 方法并打印结果

val pipeline: List<Bson> = listOf(
search(SearchOperator.text(
fieldPath("title"), "Alabama")),
project(Projections.include("title"))
)
val results = collection.aggregate(pipeline)
results.forEach { doc ->
println(doc.toJson())
}
{"_id": {"$oid": "..."}, "title": "Alabama Moon"}
{"_id": {"$oid": "..."}, "title": "Crazy in Alabama"}
{"_id": {"$oid": "..."}, "title": "Sweet Home Alabama"}

使用 searchMeta() 方法创建 $searchMeta 管道阶段,该阶段仅返回Atlas全文搜索结果中的元数据。

提示

仅适用于 Atlas for MongoDB v4.4.11 及更高版本

此聚合管道操作符仅适用于运行 v4.4.11 及更高版本的 MongoDB Atlas 集群。

The following example shows the near metadata for a MongoDB Search aggregation stage:

searchMeta(
SearchOperator.near(2010, 1, fieldPath("year"))
)

Kotlin Sync驾驶员为以下操作符提供了辅助方法:

Operator
说明

从不完整输入字符串中搜索包含字符序列的单词或短语。

将两个或多个操作符组合到一个查询中。

检查字段是否与您指定的值匹配。映射到 equals()equalsNull() 方法。

测试指定索引字段名称的路径在文档中是否存在。

在给定路径搜索由BSON数字、日期、布尔值、ObjectId、uuid 或字符串值组成的大量,并返回该字段的值等于指定大量中任意值的文档。

返回与输入文档类似的文档。

支持对数字、日期和GeoJSON point值进行查询和评分。

使用索引配置中指定的分析器搜索包含有序术语序列的文档。

支持查询索引字段和值的组合。

支持对数字、日期和字符串值进行查询和评分。 映射到 numberRange()dateRange() 方法。

将查询字段解释为正则表达式。

使用在索引配置中指定的分析器执行全文搜索。

启用在搜索字符串中使用可匹配任何字符的特殊字符的查询。

注意

Atlas样本数据集

此示例使用Atlas示例数据集中的 sample_mflix.movies集合。要学习;了解如何设立免费套餐的Atlas 集群并加载示例数据集,请参阅Atlas文档中的Atlas入门教程

在运行此示例之前,您必须在具有以下定义的 movies集合上创建MongoDB搜索索引:

{
"mappings": {
"dynamic": true,
"fields": {
"title": {
"analyzer": "lucene.keyword",
"type": "string"
},
"genres": {
"normalizer": "lowercase",
"type": "token"
}
}
}
}

要学习;了解有关创建MongoDB搜索索引的更多信息,请参阅 索引指南的创建搜索索引部分。

以下代码创建具有以下规范的 $search 阶段:

  • 检查 genres大量是否包含 "Comedy"

  • fullplot 字段中搜索字段"new york"

  • 匹配介于 19502000(含)之间的 year

  • 搜索以术语"Love" 开头的 title

val searchStage = Aggregates.search(
SearchOperator.compound()
.filter(
listOf(
SearchOperator.`in`(fieldPath("genres"), listOf("Comedy")),
SearchOperator.phrase(fieldPath("fullplot"), "new york"),
SearchOperator.numberRange(fieldPath("year")).gtLt(1950, 2000),
SearchOperator.wildcard(fieldPath("title"), "Love *")
)
)
)
val projectStage = Aggregates.project(
Projections.include("title", "year", "genres"))
val pipeline = listOf(searchStage, projectStage)
val results = collection.aggregate(pipeline)
results.forEach { result -> println(result) }
{"_id": ..., "genres": ["Comedy", "Romance"], "title": "Love at First Bite", "year": 1979}
{"_id": ..., "genres": ["Comedy", "Drama"], "title": "Love Affair", "year": 1994}

要学习;了解有关MongoDB Search 助手方法的更多信息,请参阅驱动程序核心API文档中的 SearchOperator 接口参考。

要学习;了解有关MongoDB Search 的更多信息,请参阅Atlas文档中的MongoDB Search。

要学习;了解有关本指南中提到的方法的更多信息,请参阅以下API文档:

后退

运行数据库命令

在此页面上