嵌套数组中最常见的元素

时间:2018-03-10 14:23:11

标签: arrays mongodb nosql mongodb-query aggregation-framework

我有Poem

类型的以下文档结构
{
"_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."
                        ]
                    }, 
                    // ...
            },
            // ...
        ]
    },
    // ...
]}

我需要为集合中character计数最多的每个poem找到says。 我尝试了以下查询:

db.poems.aggregate([
{$unwind:"$acts"},
{$unwind:"$acts.scenes"},
{$unwind:"$acts.scenes.action"},
{$unwind:"$acts.scenes.action.says"},
{$group: {_id: {poem: "$acts", character: "$acts.scenes.action.character", characterSaysCount: {$sum: 1}}}}])

尚未完成,我不知道下一步该做什么。

1 个答案:

答案 0 :(得分:0)

    aggregate([
    {
       $unwind: "$acts"
    },
    {
        $unwind: "$acts.scenes"
    },
    {
        $unwind: "$acts.scenes.action"
    },
    {
        $unwind: "$acts.scenes.action.says"
    },
    {
        $group: {_id: {poem: "$_id",character: "$acts.scenes.action.character"} , count:{$sum:1 }}
    },
    {
        $group: {_id: "$_id.poem", characters:{$push:{character: "$_id.character",count:"$count"}}}
    },
    {
        $sort: { "characters.count": 1 }
    },
    {
        $project: {
            poem : "$_id",
            saysCount: {$arrayElemAt: [ "$characters", 0 ]},
            _id : 0
        }
    }
])

根据您的评论更新了答案。希望这个能完美运作。