“文档” 菜单
文档首页
/
MongoDB 阿特拉斯
/ /

如何在 Atlas Search 中使用分面

在此页面上

  • 先决条件
  • 为分面创建 Atlas Search 索引
  • 搜索集合
  • 继续学习

本教程介绍如何使用面定义 sample_mflix.movies集合中的字符串、日期和数字字段创建索引。它展示了如何针对这些字段运行 Atlas Search 查询,以获取按字符串字段的值以及按日期和数字字段的范围分组的结果,包括每个组的计数。它将引导您完成以下步骤:

  1. 使用 sample_mflix.movies 集合中的 genresreleasedyear 字段的分面定义设置 Atlas Search 索引。

  2. 针对 sample_mflix.movies 集合中的 released 字段运行 Atlas Search 查询,以获取按 genres 字段的值和year 字段的范围分组的结果。

要创建 Atlas Search 索引,您必须拥有 Project Data Access Admin 或更高的项目访问权限。

要完成这些教程,除了 Atlas Search 教程页面中列出的先决条件外,您还必须有一个运行以下版本之一的 Atlas cluster:

  • MongoDB 5.0.4+

  • MongoDB 6.0+

  • MongoDB 7.0+

在本部分,您将在 sample_mflix.movies 集合中的 genresyearreleased 字段上创建 Atlas Search 索引。

1
  1. 如果尚未显示,请选择包含所需项目的组织导航栏中的Organizations菜单。

  2. 如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。

  3. 如果 Clusters(数据库部署)页面尚未出现,请单击侧边栏中的 Database(数据库)。

2

您可以从侧边栏、 Data Explorer或集群详细信息页面转到 Atlas Search 页面。

  1. 在侧边栏中,单击Services标题下的Atlas Search

  2. Select data source下拉列表中,选择您的集群并单击Go to Atlas Search

  1. 单击集群的对应 Browse Collections 按钮。

  2. 展开数据库并选择集合。

  3. 单击集合的Search Indexes标签页。

  1. 单击集群名称。

  2. 单击 Atlas Search 标签页。

3
4
  • 要获得引导式体验,请选择 Atlas Search Visual Editor

  • 要编辑原始索引定义,请选择 Atlas SearchJSON Editor

5
  1. Index Name 字段中输入 facet-tutorial

    注意

    如果将索引命名为 default,则在使用 $search 管道阶段时,您无需指定 index 参数。否则,您必须使用 index 参数指定索引名称。

  2. Database and Collection(数据库和集合)部分中找到 sample_mflix 数据库,然后选择 movies 集合。

6

您可以创建使用动态映射静态映射的 Atlas Search 索引。要了解有关动态和静态映射的更多信息,请参阅静态和动态映射

以下索引定义对 movies 集合中的支持类型字段动态创建索引。您可以使用 Atlas Search Visual Editor 或 Atlas Search JSON Editor 在 Atlas 用户界面中创建索引。

  1. 单击 Next(连接)。

  2. 查看 movies 集合的 "default" 索引定义。

  1. 单击 Next(连接)。

  2. 查看索引定义。

    索引定义应类似于以下内容:

    {
    "mappings": {
    "dynamic": true
    }
    }

    上述索引定义对 movies 集合中每个文档中的支持类型字段动态创建索引。

  3. 单击 Next(连接)。

7
8

此时将显示一个模态窗口,让您知道索引正在构建中。点击 Close 按钮。

9

构建索引大约需要一分钟时间。在构建时,Status 列显示 Build in Progress。构建完成后,Status 列显示 Active


➤ 使用选择语言下拉菜单设置本节中示例的语言。


您可以在使用 $search$searchMeta 阶段的查询中使用分面。在本部分,连接到您的 Atlas 集群并使用 $searchMeta 阶段针对 sample_mflix.movies 集合运行示例查询。MongoDB 建议仅使用 $searchMeta 阶段检索元数据结果。

1
  1. 如果尚未显示,请选择包含所需项目的组织导航栏中的Organizations菜单。

  2. 如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。

  3. 如果 Clusters(数据库部署)页面尚未出现,请单击侧边栏中的 Database(数据库)。

2

您可以从侧边栏、 Data Explorer或集群详细信息页面转到 Atlas Search 页面。

  1. 在侧边栏中,单击Services标题下的Atlas Search

  2. Select data source下拉列表中,选择您的集群并单击Go to Atlas Search

  1. 单击集群的对应 Browse Collections 按钮。

  2. 展开数据库并选择集合。

  3. 单击集合的Search Indexes标签页。

  1. 单击集群名称。

  2. 单击 Atlas Search 标签页。

3

单击要查询的索引右侧的 Query 按钮。

4

单击Edit $search Query查看 JSON格式的默认查询语法示例。

5

要运行查询,请将以下查询复制并粘贴到 Query Editor,然后单击 Search

以下查询搜索在 1921 年 11 月 11 日前后上映的电影。它要求电影相对于 originpivot 距离为大约三个月。它要求提供 genresyear 字段上的元数据。该查询要求计数:

  • genres 字符串数组字段中每种类型的电影数量

  • 1910 年至 1939 年(含)期间的电影数量

[
{
"$searchMeta": {
"index": "facet-tutorial",
"facet": {
"operator": {
"near": {
"path": "released",
"origin": ISODate("1921-11-01T00:00:00.000+00:00"),
"pivot": 7776000000
}
},
"facets": {
"genresFacet": {
"type": "string",
"path": "genres"
},
"yearFacet" : {
"type" : "number",
"path" : "year",
"boundaries" : [1910,1920,1930,1940]
}
}
}
}
}
]
6

Search Tester 可能不会显示结果中字段的所有值。要查看结果中字段的所有值,请展开字段。

Atlas Search 在页面中显示以下结果:

count: Object
lowerBound: 20878
facet: Object
genresFacet: Object
buckets: Array (10)
0: Object
_id: "Drama"
count: 12149
1: Object
_id: "Comedy"
count: 6436
2: Object
_id: "Romance"
count: 3274
3: Object
_id: "Crime"
count: 2429
4: Object
_id: "Thriller"
count: 2400
5: Object
_id: "Action"
count: 2349
6: Object
_id: "Adventure"
count: 1876
7: Object
_id: "Documentary"
count: 1755
8: Object
_id: "Horror"
count: 1432
9: Object
_id: "Biography"
count: 1244
yearFacet: Object
buckets: Array (3)
0: Object
_id: 1910
count: 14
1: Object
_id: 1920
count: 47
2: Object
_id: 1930
count: 238
1

在终端窗口中打开mongosh并连接到集群。 有关连接的详细说明,请参阅通过mongosh连接。

2

mongosh 提示符下运行以下命令:

use sample_mflix
3

以下查询搜索在 1921 年 11 月 11 日前后上映的电影。它要求电影相对于 originpivot 距离为大约三个月。它要求提供 genresyear 字段上的元数据。该查询要求计数:

  • genres 字符串数组字段中每种类型的电影数量

  • 1910 年至 1939 年(含)期间的电影数量

db.movies.aggregate([
{
"$searchMeta": {
"index": "facet-tutorial",
"facet": {
"operator": {
"near": {
"path": "released",
"origin": ISODate("1921-11-01T00:00:00.000+00:00"),
"pivot": 7776000000
}
},
"facets": {
"genresFacet": {
"type": "string",
"path": "genres"
},
"yearFacet" : {
"type" : "number",
"path" : "year",
"boundaries" : [1910,1920,1930,1940]
}
}
}
}
}
])
{
"count" : {
"lowerBound" : NumberLong(23026)
},
"facet" : {
"genresFacet" : {
"buckets" : [
{ "_id" : "Drama", "count" : NumberLong(13527) },
{ "_id" : "Comedy", "count" : NumberLong(6922) },
{ "_id" : "Romance", "count" : NumberLong(3615) },
{ "_id" : "Crime", "count" : NumberLong(2649) },
{ "_id" : "Thriller", "count" : NumberLong(2603) },
{ "_id" : "Action", "count" : NumberLong(2505) },
{ "_id" : "Documentary", "count" : NumberLong(2041) },
{ "_id" : "Adventure", "count" : NumberLong(2016) },
{ "_id" : "Horror", "count" : NumberLong(1662) },
{ "_id" : "Biography", "count" : NumberLong(1373) }
]
},
"yearFacet" : {
"buckets" : [
{ "_id" : 1910, "count" : NumberLong(23) },
{ "_id" : 1920, "count" : NumberLong(89) },
{ "_id" : 1930, "count" : NumberLong(308) }
]
}
}
}
1

打开 MongoDB Compass 并连接到您的集群。有关连接的详细说明,请参阅通过 Compass 连接。

2

Database 屏幕上,依次单击 sample_mflix 数据库和 movies 集合。

3

该查询使用以下searchMeta操作符子句:

  • near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于 originpivot 距离为大约三个月

  • facets 请求genres字符串数组字段中每种类型的电影数量以及1910至1939 (含)年份的电影数量

若要在 MongoDB Compass 中运行此查询:

  1. 单击 Aggregations 标签页。

  2. 单击 Select...,然后从下拉菜单中选择阶段并为该阶段添加查询,以配置以下每个管道阶段。单击 Add Stage 以添加其他阶段。

    管道阶段
    查询
    $searchMeta
    {
    index: 'facet-tutorial',
    facet: {
    operator: {
    near: {
    path: 'released',
    origin: ISODate("1921-11-01T00:00:00.000+00:00"),
    pivot: 7776000000
    }
    },
    facets: {
    genresFacet: {
    type: 'string',
    path: 'genres'
    },
    yearFacet: {
    type: 'number',
    path: 'year',
    boundaries: [1910,1920,1930,1940]
    }
    }
    }
    }

如果启用了 Auto Preview,MongoDB Compass 将在 $searchMeta 管道阶段旁边显示以下文档:

{
count: { lowerBound: 23026 },
facet: {
genresFacet: {
buckets: [
{ _id: "Drama", count: 13527 },
{ _id: "Comedy", count: 6922 },
{ _id: "Romance", count: 3615 },
{ _id: "Crime", count: 2649 },
{ _id: "Thriller", count: 2603 },
{ _id: "Action", count: 2505 },
{ _id: "Documentary", count: 2041 },
{ _id: "Adventure", count: 2016 },
{ _id: "Horror", count: 1662 },
{ _id: "Biography", count: 1373 }
]
},
yearFacet: {
buckets: [
{ _id: 1910, count: 23 },
{ _id: 1920, count: 89 },
{ _id: 1930, count: 308 }
]
}
}
}
1
  1. 创建一个名为facet-query-example的新目录,并使用dotnet new命令初始化项目。

    mkdir facet-query-example
    cd facet-query-example
    dotnet new console
  2. 将 .NET/C# 驱动程序作为依赖项添加到项目中。

    dotnet add package MongoDB.Driver
2
  1. Program.cs文件的内容替换为以下代码。

    以下查询搜索在 1921 年 11 月 11 日前后上映的电影。它要求电影相对于 originpivot 距离为大约三个月。它要求提供 genresyear 字段上的元数据。该查询要求计数:

    • genres 字符串数组字段中每种类型的电影数量

    • 1910 年至 1939 年(含)期间的电影数量

    1using MongoDB.Bson;
    2using MongoDB.Bson.Serialization.Attributes;
    3using MongoDB.Bson.Serialization.Conventions;
    4using MongoDB.Driver;
    5using MongoDB.Driver.Search;
    6
    7public class FacetExample
    8{
    9 private const string MongoConnectionString = "<connection-string>";
    10
    11 public static void Main(string[] args)
    12 {
    13 // allow automapping of the camelCase database fields to our MovieDocument
    14 var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
    15 ConventionRegistry.Register("CamelCase", camelCaseConvention, type => true);
    16
    17 // connect to your Atlas cluster
    18 var mongoClient = new MongoClient(MongoConnectionString);
    19 var mflixDatabase = mongoClient.GetDatabase("sample_mflix");
    20 var moviesCollection = mflixDatabase.GetCollection<MovieDocument>("movies");
    21
    22 // declare data for date and number fields
    23 var originDate = new DateTime(1921, 11, 01, 0, 0, 0, DateTimeKind.Utc);
    24
    25 // define and run pipeline
    26 var results = moviesCollection.Aggregate()
    27 .SearchMeta(Builders<MovieDocument>.Search.Facet(
    28 Builders<MovieDocument>.Search.Near(movie => movie.Released, originDate, 7776000000),
    29 Builders<MovieDocument>.SearchFacet.String("genresFacet", movie => movie.Genres, 10),
    30 Builders<MovieDocument>.SearchFacet.Number("yearFacet", movie => movie.Year, 1910, 1920, 1930, 1940)),
    31 indexName: "facet-tutorial")
    32 .Single();
    33
    34 // print results
    35 Console.WriteLine(results.ToJson());
    36 }
    37}
    38
    39[BsonIgnoreExtraElements]
    40public class MovieDocument
    41{
    42 [BsonIgnoreIfDefault]
    43 public ObjectId Id { get; set; }
    44 public string [] Genres { get; set; }
    45 public DateTime Released { get; set; }
    46 public int Year { get; set; }
    47}
  2. 在运行示例之前,请将 <connection-string> 替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。

3
dotnet run facet-query-example.csproj
{
"count" : { "lowerBound" : NumberLong(23026), "total" : null },
"facet" : {
"genresFacet" : {
"buckets" : [
{ "_id" : "Drama", "count" : NumberLong(13527) },
{ "_id" : "Comedy", "count" : NumberLong(6922) },
{ "_id" : "Romance", "count" : NumberLong(3615) },
{ "_id" : "Crime", "count" : NumberLong(2649) },
{ "_id" : "Thriller", "count" : NumberLong(2603) },
{ "_id" : "Action", "count" : NumberLong(2505) },
{ "_id" : "Documentary", "count" : NumberLong(2041) },
{ "_id" : "Adventure", "count" : NumberLong(2016) },
{ "_id" : "Horror", "count" : NumberLong(1662) },
{ "_id" : "Biography", "count" : NumberLong(1373) }
]
},
"yearFacet" : {
"buckets" : [
{ "_id" : 1910, "count" : NumberLong(23) },
{ "_id" : 1920, "count" : NumberLong(89) },
{ "_id" : 1930, "count" : NumberLong(308) }
]
}
}
}
1
  1. 创建一个名为 facet-query.go 的文件。

  2. 将以下代码复制并粘贴到 facet-query.go 文件。

    此代码示例将执行以下任务:

    • 导入mongodb包和依赖项。

    • 建立与您的 Atlas 集群的连接。

    • 使用以下 searchMeta 子句来查询集合:

      • near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于 originpivot 距离为大约三个月

      • facets 请求genres字符串数组字段中每种类型的电影数量以及1910至1939 (含)年份的电影数量

    • 遍历游标以打印与查询匹配的文档。

    1package main
    2
    3import (
    4 "context"
    5 "fmt"
    6 "time"
    7
    8 "go.mongodb.org/mongo-driver/bson"
    9 "go.mongodb.org/mongo-driver/mongo"
    10 "go.mongodb.org/mongo-driver/mongo/options"
    11)
    12
    13func main() {
    14 // connect to your Atlas cluster
    15 client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("<connection-string>"))
    16 if err != nil {
    17 panic(err)
    18 }
    19 defer client.Disconnect(context.TODO())
    20
    21 // set namespace
    22 collection := client.Database("sample_mflix").Collection("movies")
    23
    24 // define pipeline stages
    25 searchStage := bson.D{{"$searchMeta", bson.M{
    26 "index": "facet-tutorial",
    27 "facet": bson.M{
    28 "operator": bson.M{
    29 "near": bson.M{
    30 "path": "released",
    31 "origin": time.Date(1921, time.November, 1, 0, 0, 0, 0, time.UTC),
    32 "pivot": 7776000000},
    33 },
    34 "facets": bson.M{
    35 "genresFacet": bson.M{
    36 "path": "genres",
    37 "type": "string"},
    38 "yearFacet": bson.M{
    39 "path": "year",
    40 "type": "number",
    41 "boundaries": bson.A{1910, 1920, 1930, 1940}},
    42 }}}}}
    43 // run pipeline
    44 cursor, err := collection.Aggregate(context.TODO(), mongo.Pipeline{searchStage})
    45 if err != nil {
    46 panic(err)
    47 }
    48
    49 // print results
    50 var results []bson.D
    51 if err = cursor.All(context.TODO(), &results); err != nil {
    52 panic(err)
    53 }
    54 for _, result := range results {
    55 fmt.Println(result)
    56 }
    57}
  3. 在运行示例之前,请将 <connection-string> 替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。

  4. 运行以下命令来查询您的集合:

    go run facet-query.go
    [{count [{lowerBound 23026}]}
    {facet [
    {genresFacet [
    {buckets [
    [{_id Drama} {count 13527}]
    [{_id Comedy} {count 6922}]
    [{_id Romance} {count 3615}]
    [{_id Crime} {count 2649}]
    [{_id Thriller} {count 2603}]
    [{_id Action} {count 2505}]
    [{_id Documentary} {count 2041}]
    [{_id Adventure} {count 2016}]
    [{_id Horror} {count 1662}]
    [{_id Biography} {count 1373}]]}]}
    {yearFacet [
    {buckets [
    [{_id 1910} {count 23}]
    [{_id 1920} {count 89}]
    [{_id 1930} {count 308}]]}]}]}]
1
junit
4.11 或更高版本
mongodb-driver-sync
4.3.0 或更高版本
slf4j-log4j12
1.7.30 或更高版本
2
  1. 创建一个名为 FacetQuery.java 的文件。

  2. 将以下代码复制并粘贴到 FacetQuery.java 文件。

    此代码示例将执行以下任务:

    • 导入mongodb包和依赖项。

    • 建立与您的 Atlas 集群的连接。

    • 使用以下 searchMeta 子句来查询集合:

      • near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于 originpivot 距离为大约三个月

      • facets 请求genres字符串数组字段中每种类型的电影数量以及1910至1939 (含)年份的电影数量

    • 遍历游标以打印与查询匹配的文档。

    1import com.mongodb.client.MongoClient;
    2import com.mongodb.client.MongoClients;
    3import com.mongodb.client.MongoCollection;
    4import com.mongodb.client.MongoDatabase;
    5import org.bson.Document;
    6
    7import java.time.Instant;
    8import java.util.Arrays;
    9import java.util.Date;
    10
    11public class FacetQuery {
    12 public static void main(String[] args) {
    13 // connect to your Atlas cluster
    14 String uri = "<connection-string>";
    15
    16 try (MongoClient mongoClient = MongoClients.create(uri)) {
    17 // set namespace
    18 MongoDatabase database = mongoClient.getDatabase("sample_mflix");
    19 MongoCollection<Document> collection = database.getCollection("movies");
    20
    21 // define pipeline
    22 Document agg = new Document("$searchMeta",
    23 new Document( "index", "facet-tutorial")
    24 .append("facet",
    25 new Document("operator",
    26 new Document("near",
    27 new Document("path", "released")
    28 .append("origin", Date.from(Instant.parse("1921-11-01T00:00:00.000+00:00")))
    29 .append("pivot", 7776000000L)))
    30 .append("facets",
    31 new Document("genresFacet",
    32 new Document("type", "string").append("path", "genres"))
    33 .append("yearFacet",
    34 new Document("type", "number").append("path", "year")
    35 .append("boundaries", Arrays.asList(1910, 1920, 1930, 1940))
    36 ))));
    37 // run pipeline and print results
    38 collection.aggregate(Arrays.asList(agg))
    39 .forEach(doc -> System.out.println(doc.toJson()));
    40
    41 }
    42 }
    43}

    注意

    要在 Maven 环境中运行示例代码,请在您的声明中的 import 文件上方添加以下内容。

    package com.mongodb.drivers;
  3. 在运行示例之前,请将 <connection-string> 替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。

  4. 编译并运行FacetQuery.java文件。

    javac FacetQuery.java
    java FacetQuery
    {"count": {"lowerBound": 23026},
    "facet": {
    "genresFacet": {
    "buckets": [
    {"_id": "Drama", "count": 13527},
    {"_id": "Comedy", "count": 6922},
    {"_id": "Romance", "count": 3615},
    {"_id": "Crime", "count": 2649},
    {"_id": "Thriller", "count": 2603},
    {"_id": "Action", "count": 2505},
    {"_id": "Documentary", "count": 2041},
    {"_id": "Adventure", "count": 2016},
    {"_id": "Horror", "count": 1662},
    {"_id": "Biography", "count": 1373}]},
    "yearFacet": {
    "buckets": [
    {"_id": 1910, "count": 23},
    {"_id": 1920, "count": 89},
    {"_id": 1930, "count": 308}]}}}
1
mongodb-driver-kotlin-coroutine
4.10.0 或更高版本
2
  1. 创建一个名为 FacetQuery.kt 的文件。

  2. 将以下代码复制并粘贴到 FacetQuery.kt 文件。

    此代码示例将执行以下任务:

    • 导入mongodb包和依赖项。

    • 建立与您的 Atlas 集群的连接。

    • 使用以下searchMeta子句查询集合:

      • near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于 originpivot 距离为大约三个月

      • facets 请求genres字符串数组字段中每种类型的电影数量以及1910至1939 (含)年份的电影数量

    • 打印与 AggregateFlow 实例中的查询相匹配的文档。

    1import com.mongodb.kotlin.client.coroutine.MongoClient
    2import kotlinx.coroutines.runBlocking
    3import org.bson.Document
    4import java.time.Instant
    5import java.util.*
    6
    7fun main() {
    8 // connect to your Atlas cluster
    9 val uri = "<connection-string>"
    10 val mongoClient = MongoClient.create(uri)
    11
    12 // set namespace
    13 val database = mongoClient.getDatabase("sample_mflix")
    14 val collection = database.getCollection<Document>("movies")
    15
    16 runBlocking {
    17 // define pipeline
    18 val agg = Document(
    19 "\$searchMeta",
    20 Document("index", "facet-tutorial")
    21 .append(
    22 "facet",
    23 Document(
    24 "operator",
    25 Document(
    26 "near",
    27 Document("path", "released")
    28 .append("origin", Date.from(Instant.parse("1921-11-01T00:00:00.000+00:00")))
    29 .append("pivot", 7776000000L)
    30 )
    31 )
    32 .append(
    33 "facets",
    34 Document(
    35 "genresFacet",
    36 Document("type", "string").append("path", "genres")
    37 )
    38 .append(
    39 "yearFacet",
    40 Document("type", "number").append("path", "year")
    41 .append("boundaries", listOf(1910, 1920, 1930, 1940))
    42 )
    43 )
    44 )
    45 )
    46
    47 // run pipeline and print results
    48 val resultsFlow = collection.aggregate<Document>(
    49 listOf(agg)
    50 )
    51 resultsFlow.collect { println(it) }
    52 }
    53 mongoClient.close()
    54}
  3. 在运行示例之前,请将 <connection-string> 替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。

  4. 运行 FacetQuery.kt 文件。

    当您在 IDE 中运行FacetQuery.kt程序时,它会打印以下结果:

    Document{{count=Document{{lowerBound=20878}},
    facet=Document{{genresFacet=Document{{buckets=[Document{{_id=Drama,
    count=12149}}, Document{{_id=Comedy, count=6436}},
    Document{{_id=Romance, count=3274}}, Document{{_id=Crime,
    count=2429}}, Document{{_id=Thriller, count=2400}},
    Document{{_id=Action, count=2349}}, Document{{_id=Adventure,
    count=1876}}, Document{{_id=Documentary, count=1755}},
    Document{{_id=Horror, count=1432}}, Document{{_id=Biography,
    count=1244}}]}},
    yearFacet=Document{{buckets=[Document{{_id=1910, count=14}},
    Document{{_id=1920, count=47}}, Document{{_id=1930,
    count=238}}]}}}}}}
1
  1. 创建一个名为 facet-query.js 的文件。

  2. 将以下代码复制并粘贴到 facet-query.js 文件。

    此代码示例将执行以下任务:

    • 导入 mongodb,即 MongoDB 的 Node.js 驱动程序。

    • 创建一个 MongoClient 类实例,以建立与 Atlas 集群的连接。

    • 使用以下 searchMeta 子句来查询集合:

      • near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于 originpivot 距离为大约三个月

      • facets 请求genres字符串数组字段中每种类型的电影数量以及1910至1939 (含)年份的电影数量

    • 遍历游标以打印与查询匹配的文档。

    1const { MongoClient } = require("mongodb");
    2
    3// connect to your Atlas cluster
    4const uri =
    5 "<connection-string>";
    6
    7const client = new MongoClient(uri);
    8
    9async function run() {
    10 try {
    11 await client.connect();
    12
    13 // set namespace
    14 const database = client.db("sample_mflix");
    15 const coll = database.collection("movies");
    16
    17 // define pipeline
    18 const agg = [{$searchMeta: {
    19 index: "facet-tutorial",
    20 facet: {
    21 operator: {
    22 near: {path: "released", origin: new Date("1921-11-01T00:00:00.000Z"), pivot: 7776000000}
    23 },
    24 facets: {
    25 genresFacet: {type: "string", path: "genres"},
    26 yearFacet: {type: "number", path: "year",boundaries: [1910,1920,1930,1940]}
    27 }}}}];
    28 // run pipeline
    29 const result = coll.aggregate(agg);
    30
    31 // print results
    32 await result.forEach((doc) => console.dir(JSON.stringify(doc)));
    33 } finally {
    34 await client.close();
    35 }
    36}
    37run().catch(console.dir);
  3. 在运行示例之前,请将 <connection-string> 替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。

  4. 运行以下命令来查询您的集合:

    node facet-query.js
    '{"count":{"lowerBound":23026},
    "facet":{
    "genresFacet":{
    "buckets":[
    {"_id":"Drama","count":13527},
    {"_id":"Comedy","count":6922},
    {"_id":"Romance","count":3615},
    {"_id":"Crime","count":2649},
    {"_id":"Thriller","count":2603},
    {"_id":"Action","count":2505},
    {"_id":"Documentary","count":2041},
    {"_id":"Adventure","count":2016},
    {"_id":"Horror","count":1662},
    {"_id":"Biography","count":1373}]},
    "yearFacet":{
    "buckets":[
    {"_id":1910,"count":23},
    {"_id":1920,"count":89},
    {"_id":1930,"count":308}]}}}'
1
  1. 创建一个名为 facet-query.py 的文件。

  2. 将以下代码复制并粘贴到 facet.py 文件。

    以下代码示例:

    • 导入pymongo 、MongoDB 的 Python 驱动程序和dns模块,这是使用DNS种子列表连接字符串将pymongo连接到Atlas所必需的。

    • 创建一个 MongoClient 类实例,以建立与 Atlas 集群的连接。

    • 使用以下 searchMeta 子句来查询集合:

      • near 搜索 1921 年 11 月 11 日前后上映的电影,要求电影相对于 originpivot 距离为大约三个月

      • facets 请求genres字符串数组字段中每种类型的电影数量以及1910至1939 (含)年份的电影数量

    • 遍历游标以打印与查询匹配的文档。

    1import pymongo
    2import datetime
    3
    4# connect to your Atlas cluster
    5client = pymongo.MongoClient('<connection-string>')
    6
    7# define pipeline
    8pipeline = [{"$searchMeta": {
    9 "index": "facet-tutorial",
    10 "facet": {
    11 "operator": {
    12 "near": {"path": "released", "origin": datetime.datetime(1921, 11, 1, 0, 0, 0, 0), "pivot": 7776000000}
    13 },
    14 "facets": {
    15 "genresFacet": {"type": "string", "path": "genres"},
    16 "yearFacet" : {"type" : "number", "path" : "year", "boundaries" : [1910,1920,1930,1940]}
    17 }}}}]
    18# run pipeline
    19result = client["sample_mflix"]["movies"].aggregate(pipeline)
    20
    21# print results
    22for i in result:
    23 print(i)
  3. 在运行示例之前,请将 <connection-string> 替换为 Atlas 连接字符串。确保您的连接字符串包含数据库用户的档案。要了解详情,请参阅通过驱动程序连接。

  4. 运行以下命令来查询您的集合:

    python facet-query.py
    {'count': {'lowerBound': 23026},
    'facet': {
    'genresFacet': {
    'buckets': [
    {'count': 13527, '_id': 'Drama'},
    {'count': 6922, '_id': 'Comedy'},
    {'count': 3615, '_id': 'Romance'},
    {'count': 2649, '_id': 'Crime'},
    {'count': 2603, '_id': 'Thriller'},
    {'count': 2505, '_id': 'Action'},
    {'count': 2041, '_id': 'Documentary'},
    {'count': 2016, '_id': 'Adventure'},
    {'count': 1662, '_id': 'Horror'},
    {'count': 1373, '_id': 'Biography'}]},
    'yearFacet': {
    'buckets': [
    {'count': 23, '_id': 1910},
    {'count': 89, '_id': 1920},
    {'count': 308, '_id': 1930}]}}}

结果显示两种类型的分面搜索的元数据结果。genresFacet 文档显示每类电影的数量,yearFacet 文档显示边界内的电影数量:

  • 1910,包括 1910 存储桶的下限

  • 19201910 存储桶不包括上边界,1920 存储桶包括下边界

  • 19301920 存储桶不包括上边界,1930 存储桶包括下边界

您可以通过我们的课程视频了解有关 Atlas Search 中的分面的更多信息。

要了解有关在 Atlas Search 中使用分面的更多信息,请学习MongoDB University 的 MongoDB 简介课程的9单元。 1 。 5小时的单元包括 Atlas Search 概述以及有关创建 Atlas Search 索引、使用复合运算符运行$search查询以及使用facet对结果进行分组的课程。

观看此视频,了解如何在查询中创建和使用数字和字符串分,对结果进行分组并检索分组中的结果计数。

时长:11 分钟

后退

自动完成(Autocomplete)

来年

同义词