arrays - MongoDB统计最常见的嵌套数组

标签 arrays mongodb mongodb-query aggregation-framework

我有诗集。集合中的文档具有以下结构:

{
"_id" : "Romeo and Juliet",
"acts" : [ 
    {
        "title" : "ACT I",
        "scenes" : [ 
            {
                "title" : "SCENE I. Verona. A public place.",
                "action" : [ 
                    {
                        "character" : "SAMPSON",
                        "says" : [ 
                            "Gregory, o' my word, we'll not carry coals."
                        ]
                    }, 
                    {
                        "character" : "GREGORY",
                        "says" : [ 
                            "No, for then we should be colliers."
                        ]
                    }, 
                    // ...
                    {
                        "character" : "GREGORY",
                        "says" : [ 
                            "To move is to stir; and to be valiant is to stand:", 
                            "therefore, if thou art moved, thou runn'st away."
                        ]
                    }, 
                    {
                        "character" : "SAMPSON",
                        "says" : [ 
                            "A dog of that house shall move me to stand: I will", 
                            "take the wall of any man or maid of Montague's."
                        ]
                    }, 
                    {
                        "character" : "GREGORY",
                        "says" : [ 
                            "That shows thee a weak slave; for the weakest goes", 
                            "to the wall."
                        ]
                    }, 
                    // ...
            },
            // ...
        ]
    },
    // ...
]}

我需要在scene对象中找到对话数量最多的诗(says)。 我尝试过使用 $group$unwind$sort 等,但结果并不正确。

最佳答案

I need to find the poem with the most number of dialog (says) in the scene object

这可以用以下方式解释。

Count all says across all action in all scenes object for all acts in each poem and show the poem document which has the max says across all poems.

这个想法是找到所有 Action 、所有场景和所有行为的最大说,并输出具有最大说的诗歌文档。

嵌套$map$max组合来输出不同级别的最大值。

$project 排除“maxsaysacrossallacts”字段。

类似

 db.poems.aggregate([
  {"$addFields":{
    "maxsaysacrossallacts":{
      "$max":{
        "$map":{
          "input":"$acts",
          "as":"maxsaysineachact",
          "in":{
            "$max":{
              "$map":{
                "input":"$$maxsaysineachact.scenes",
                "as":"maxsaysineachscene",
                "in":{
                  "$max":{
                    "$map":{
                      "input":"$$maxsaysineachscene.action",
                      "as":"sayssceneineachaction",
                      "in":{"$size":"$$sayssceneineachaction.says"}
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }},
  {"$sort":{"maxsaysacrossallacts":-1}},
  {"$limit":1},
  {"$project":{"maxsaysacrossallacts":0}}
])

更新:根据下面的评论,如果您需要输出最大说诗文档,您可以使用下面的聚合查询。

计算所有行为中的所有说法,并输出具有最大说法计数的诗歌。

db.poems.aggregate([
  {"$addFields":{
    "sumsaysacrossallacts":{
      "$sum":{
        "$map":{
          "input":"$acts",
          "as":"sumsaysineachact",
          "in":{
            "$sum":{
              "$map":{
                "input":"$$sumsaysineachact.scenes",
                "as":"sumsaysineachscene",
                "in":{
                  "$sum":{
                    "$map":{
                      "input":"$$sumsaysineachscene.action",
                      "as":"sayssceneineachaction",
                      "in":{"$size":"$$sayssceneineachaction.says"}
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }},
  {"$sort":{"sumsaysacrossallacts":-1}},
  {"$limit":1},
  {"$project":{"sumsaysacrossallacts":0}}
])

关于arrays - MongoDB统计最常见的嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48731111/

相关文章:

mongodb - 如何查询嵌套对象?

arrays - 派生类型数组 : select entry

arrays - 将所有数组值转换为哈希值?

mongodb - Meteor.js – 基于Session变量的$或mongodb查询

mongodb - 您如何获取集合中的最后一个文档

java - 在 mongodb 聚合中查找

ruby - 重写一个 "better"代码

C 程序数组超过 1 个字

mongodb - Spark无法使用mongo-hadoop-connector的BSONFileInputFormat编译newAPIHadoopRDD

mongodb - 在 MongoDB 聚合中对嵌套字段使用 $multiply