【问题标题】:How to sum the values from a field in subdocuments from an array?如何对数组中子文档中字段的值求和?
【发布时间】:2021-06-12 15:04:42
【问题描述】:

如何从提供的任何关键字中仅投影在每个文档中找到的单词?文档结构如下:

{
    _id: 24752893,
    dictionary: [
        {
            word: 'word1',
            count: 2,
        },
        {   
            word: 'word2',
            count: 5,
        },
        {
            word: 'word4',
            count: 1,
        },
        ....
    ]
},
{
    _id: 6786765789,
    dictionary: [
        {
            word: 'word4',
            count: 3,
        },
        {
            word: 'word2',
            count: 6,
        },
        {
            word: 'word3',
            count: 3,
        },
        {
            word: 'word5',
            count: 1,
        },
        ....
    ]
},
........
{
    _id: 76675567,
    dictionary: [
        {
            word: 'word1',
            count: 7,
        },
        {
            word: 'word3',
            count: 2,
        },
        ....
    ]
}

如果给出了像 ['word2', 'word3'] 这样的关键字列表,并且只要在其中找到关键字列表中的任何单词,就应该检索文档。我已经编写了这个聚合管道来获取必要的文档:

client.database.collection.aggregate([
    {
    '$project': {
        '_id': 1,
        'dictionary': {
            '$filter': {
                'input': '$dictionary',
                'as': 'words',
                'cond': {
                    '$in': [
                        '$$words.word', keywords
                    ]
                }
            }
        },
    }
},
{
    '$match': {
        'dictionary': {
            '$ne': []
        }
    }
},
,
{
    '$unwind': '$dictionary'
},
{
    '$group': {
        '_id': '$_id',
        'score': {
            '$sum': '$dictionary.count'
        }
    }
}
])

我想要做的不是投影整个字典,我只想投影每个文档的匹配单词及其计数。当然,我希望每个文档的字典位于单独的投影文档中。有没有办法做到这一点?

【问题讨论】:

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


    【解决方案1】:

    使用$filter 过滤你的数组,试试这个:

    let keywords = ['word2', 'word3']
    
    db.collection.aggregate([
        {
            $project: {
                _id: 0,
                dictionary: {
                    $filter: {
                        input: "$dictionary",
                        as: "word",
                        cond: {
                            $in: ["$$word.word", keywords]
                        }
                    }
                }
            }
        },
        {
            $match: {
                $expr: {
                    $gt: [{ $size: "$dictionary" }, 0]
                }
            }
        }
    ]);
    

    输出:

    /* 1 */
    {
        "dictionary" : [
            {
                "word" : "word2",
                "count" : 5
            }
        ]
    },
    
    /* 2 */
    {
        "dictionary" : [
            {
                "word" : "word2",
                "count" : 6
            },
            {
                "word" : "word3",
                "count" : 3
            }
        ]
    },
    
    /* 3 */
    {
        "dictionary" : [
            {
                "word" : "word3",
                "count" : 2
            }
        ]
    }
    

    【讨论】:

    • 此管道提供了正确的结果,但它还返回了关键字不存在且带有空数组的文档。知道如何解决这个问题吗?
    • 放置一个$mtch 条件并检查空数组。
    • 我可以得到每个字典字段的值的总和吗?我尝试在“_id”字段上使用 $group 阶段来做到这一点,但我找不到为每个文档获取单独的计数总和的方法。我已经更改了我尝试过的管道。
    • 你的 MongoDb 版本是多少?
    • 我用的是4.4.4版本,最新版
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多