美文网首页mongodb基础操作
mongodb Aggregation聚合操作之$facet

mongodb Aggregation聚合操作之$facet

作者: 蚁族的乐土 | 来源:发表于2021-03-17 19:09 被阅读0次

    在上一篇mongodb Aggregation聚合操作之$collStats中详细介绍了mongodb聚合操作中的$collStats使用以及参数细节。本篇将开始介绍Aggregation聚合操作中的$facet操作。

    说明:

    在同一组输入文档的单一阶段中处理多个聚合管道。每个子管道在输出文档中都有自己的字段,其结果存储在文档数组中。$facet阶段允许您在单个聚合阶段内创建多面聚合,这些聚合描述了跨多个维度(或多个方面)的数据。多面聚合提供多个过滤器和分类来指导数据浏览和分析,$facet在同一组输入文档上支持各种聚合,而不需要多次检索输入文档。

    语法:

    { $facet:

       {

          <outputField1>: [ <stage1>, <stage2>, ... ],

          <outputField2>: [ <stage1>, <stage2>, ... ],

          ...

       }

    }

    注意:

    $facet阶段及其子管道不能使用索引,即使它的子管道使用$match,或者$facet是管道中的第一阶段。$facet阶段将始终在执行期间执行COLLSCAN。

    1. 示例

    初始化数据:

    db.artwork.insertMany([{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926,

      "price" : NumberDecimal("199.99"),

      "tags" : [ "painting", "satire", "Expressionism", "caricature" ] },

    { "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902,

      "price" : NumberDecimal("280.00"),

      "tags" : [ "woodcut", "Expressionism" ] },

    { "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925,

      "price" : NumberDecimal("76.04"),

      "tags" : [ "oil", "Surrealism", "painting" ] },

    { "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai",

      "price" : NumberDecimal("167.30"),

      "tags" : [ "woodblock", "ukiyo-e" ] },

    { "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931,

      "price" : NumberDecimal("483.00"),

      "tags" : [ "Surrealism", "painting", "oil" ] },

    { "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913,

      "price" : NumberDecimal("385.00"),

      "tags" : [ "oil", "painting", "abstract" ] },

    { "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893,

      "tags" : [ "Expressionism", "painting", "oil" ] },

    { "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918,

      "price" : NumberDecimal("118.42"),

      "tags" : [ "abstract", "painting" ] }])

    示例:

    db.artwork.aggregate( [

      {

        $facet: {

          "categorizedByTags": [

            { $unwind: "$tags" },

            { $sortByCount: "$tags" }

          ],

          "categorizedByPrice": [

            // Filter out documents without a price e.g., _id: 7

            { $match: { price: { $exists: 1 } } },

            {

              $bucket: {

                groupBy: "$price",

                boundaries: [  0, 150, 200, 300, 400 ],

                default: "Other",

                output: {

                  "count": { $sum: 1 },

                  "titles": { $push: "$title" }

                }

              }

            }

          ],

          "categorizedByYears(Auto)": [

            {

              $bucketAuto: {

                groupBy: "$year",

                buckets: 4

              }

            }

          ]

        }

      }

    ])

    结果:

    [ { categorizedByTags:

         [ { _id: 'painting', count: 6 },

           { _id: 'oil', count: 4 },

           { _id: 'Expressionism', count: 3 },

           { _id: 'abstract', count: 2 },

           { _id: 'Surrealism', count: 2 },

           { _id: 'ukiyo-e', count: 1 },

           { _id: 'woodblock', count: 1 },

           { _id: 'woodcut', count: 1 },

           { _id: 'satire', count: 1 },

           { _id: 'caricature', count: 1 } ],

        categorizedByPrice:

         [ { _id: 0, count: 2, titles: [ 'Dancer', 'Blue Flower' ] },

           { _id: 150,

             count: 2,

             titles: [ 'The Pillars of Society', 'The Great Wave off Kanagawa' ] },

           { _id: 200, count: 1, titles: [ 'Melancholy III' ] },

           { _id: 300, count: 1, titles: [ 'Composition VII' ] },

           { _id: 'Other',

             count: 1,

             titles: [ 'The Persistence of Memory' ] } ],

        'categorizedByYears(Auto)':

         [ { _id: { min: null, max: 1902 }, count: 2 },

           { _id: { min: 1902, max: 1918 }, count: 2 },

           { _id: { min: 1918, max: 1926 }, count: 2 },

           { _id: { min: 1926, max: 1931 }, count: 2 } ] } ]

    相关文章

      网友评论

        本文标题:mongodb Aggregation聚合操作之$facet

        本文链接:https://www.haomeiwen.com/subject/uyiecltx.html