MongoDB 差异化聚合

Mehvish Ashiq 2023年1月30日
  1. MongoDB 差异化聚合
  2. 使用 distinct() 方法在 MongoDB 中查找不同的值
  3. 使用 distinct 命令在 MongoDB 中查找不同的值
  4. 使用 $group 聚合运算符在 MongoDB 中查找不同的值
MongoDB 差异化聚合

本文讨论了 MongoDB 的独特聚合。在本教程中,我们将学习不同的方法,我们可以使用这些方法来根据项目要求找到唯一/不同的值。

MongoDB 差异化聚合

我们必须有一个填充的集合来学习 MongoDB 差异化聚合。为此,我们创建了一个名为 collection 的集合,其中包含你可以使用的示例数据。

示例代码:

// MongoDB version 5.0.8

//insert documents
> db.collection.insertMany([
    { "_id": 1, "state": "punjab", "city": "lahore", "zipcode" : 94608 },
    { "_id": 2, "state": "punjab", "city": "lahore", "zipcode" : 94608 },
    { "_id": 3, "state": "punjab", "city": "lahore", "zipcode" : 99559 },
    { "_id": 4, "state": "punjab", "city": "lahore", "zipcode" : 99559 },

    { "_id": 5, "state": "punjab", "city": "karachi", "zipcode" : 99523 },
    { "_id": 6, "state": "punjab", "city": "karachi", "zipcode" : 94608 },
    { "_id": 7, "state": "punjab", "city": "karachi", "zipcode" : 99559 },
    { "_id": 8, "state": "punjab", "city": "karachi", "zipcode" : 99545 },

    { "_id": 9, "state": "punjab", "city": "multan", "zipcode" : 99545 },
    { "_id": 10, "state": "punjab", "city": "multan", "zipcode" : 94608 },
    { "_id": 11, "state": "punjab", "city": "multan", "zipcode" : 99559 },
    { "_id": 12, "state": "punjab", "city": "multan", "zipcode" : 99559 }
]);

你还可以执行以下命令来查看插入的数据。

// MongoDB version 5.0.8

//display documents
> db.collection.find();

使用 distinct() 方法在 MongoDB 中查找不同的值

示例代码 1:

// MongoDB version 5.0.8

// return the distinct values for the `zipcode` field across the collection
> db.collection.distinct( "zipcode");

输出:

[ 94608, 99523, 99545, 99559 ]

示例代码 2:

// MongoDB version 5.0.8

// returns unique `city` names where the `zipcode` is either 99559 or 99545 or both.
> db.collection.distinct( "city", {"zipcode": { $in: [99559,99545]}} );

输出:

[ "karachi", "lahore", "multan" ]

示例代码 3:

// MongoDB version 5.0.8

// use the `.length` property to obtain the size/length of the retrieved unique values
> db.collection.distinct( "city", {"zipcode": { $in: [99559,99545]}} ).length;

输出:

3

distinct() 方法从一个集合或视图中提取指定字段的不同值,并以数组的形式返回输出。

它至少需要一个参数,最多三个参数,其中第一个、第二个和第三个参数分别是 fieldqueryoptions

field 参数是我们应该为其返回唯一(不同)值的 field 名称。query 告诉我们需要从中获取不同值的文档。

虽然 options 参数是可选的,但它是一个指定 options 的文档。使用此方法的人必须牢记以下几点:

  1. db.collection.distinct() 提供了对 distinct 命令的包装,我们将在下一节中介绍。
  2. 如果指定字段的值是一个数组,那么 db.collection.distinct() 会将数组的每个元素视为一个单独的值。例如,如果字段包含其值为 [2, [2], 2],则 db.collection.distinct() 将采用 2[2]2 作为单独的值。

使用 distinct 命令在 MongoDB 中查找不同的值

示例代码:

// MongoDB version 5.0.8

// find distinct values
> db.runCommand ( { distinct: "collection", key: "zipcode" } );

输出:

{ "values" : [ 94608, 99523, 99545, 99559 ], "ok" : 1 }

在此代码中,我们使用了 distinct 聚合命令,该命令在一个集合中查找指定字段的所有不同值。结果,它返回一个文档,其中包含一组不同的值。

它还有一个包含查询计划和查询统计信息的嵌入式文档。

使用 $group 聚合运算符在 MongoDB 中查找不同的值

示例代码:

// MongoDB version 5.0.8

// find size of distinct values for each `city`
> db.collection.aggregate([
    {
        $group:{
            "_id": "$city",
            "unique_count": {
                $addToSet: "$zipcode"
             }
        }
    },
    {
        $project:{
            "distinct_zipcodes":{
                $size: "$unique_count"
             }
        }
    }
]);

输出:

{ "_id" : "karachi", "distinct_zipcodes" : 4 }
{ "_id" : "lahore", "distinct_zipcodes" : 2 }
{ "_id" : "multan", "distinct_zipcodes" : 3 }

此代码示例返回每个 cityzipcode 字段的不同值。阅读以下几点以了解代码中发生了什么。

  1. $addToSet 不断向数组添加值,直到该值已经存在。 $addToSet 聚合返回一个数组,其中包含我们保存在 unique_count 变量中的所有唯一值。
  2. 我们将 city 名称设为 _id,并使用 $group 聚合运算符使用 _idunique_count 对它们进行分组。
  3. 接下来,我们使用 $project 来指定字段的包含/抑制,添加新字段并重置现有字段的值。在这里,我们使用 $size 来查找名为 $unique_count 的数组的大小,并将其保存到 distinct_zipcodes 变量中,该变量进一步显示在 _id 字段旁边的屏幕上(参见上面给出的输出)。
作者: Mehvish Ashiq
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook