【问题标题】:Sum all the fields in documents in MongoDB总结MongoDB中文档中的所有字段
【发布时间】:2022-12-04 12:03:42
【问题描述】:

I have documents with a number of tokens in text, one document for a text. I need to calculate a total token count across documents. I don\'t know the exact number and keys of these fields, so I cannot use $sum for each field individually.

For example, I have two documents:

{
    \"count\": {
        \"a\": 1,
        \"b\": 5,
        \"c\": 7
    }
}

{
    \"count\": {
        \"a\": 4,
        \"c\": 2,
        \"d\": 6
    }
}

I want to aggregate them and get

{
    \"count\": {
        \"a\": 5,
        \"b\": 5,
        \"c\": 9,
        \"d\": 6
    }
}

As I understand, it is not possible, but I just wanted to make sure

    标签: mongodb aggregation-framework


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2022-11-24
    • 2020-04-20
    相关资源
    最近更新 更多