mongoDB迭代文档的键&;和值

mongoDB迭代文档的键&;和值,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我对MongoDb很陌生。话虽如此,我的csv文件数据如下,是关于每年的费用 {"Name": "Aruba", "Code": "ABW", "Type": "Country", "IndicatorName": "Military_expenditure", "1900": 0, "1961": 1, "1962": 0, "1963": 0, "1964": 0, "1965": 0, "1966": 0, "1967":

我对MongoDb很陌生。话虽如此,我的csv文件数据如下,是关于每年的费用

{"Name": "Aruba",
   "Code": "ABW",
   "Type": "Country",
   "IndicatorName": "Military_expenditure",
   "1900": 0,
   "1961": 1,
   "1962": 0,
   "1963": 0,
   "1964": 0,
   "1965": 0,
   "1966": 0,
   "1967": 0,
   "1968": 0,
   "1969": 0
}, {
   "Name": "Afghanistan",
   "Code": "AFG",
   "Type": "Country",
   "IndicatorName": "Military_expenditure",
   "1900": 0,
   "1961": 100,
   "1962": 0,
   "1963": 0,
   "1964": 0,
   "1965": 0,
   "1966": 0,
   "1967": 0,
   "1968": 0,
   "1969": 0
}
但是,我需要得到

  • 年度总开支,即
  • 阿鲁巴=1>>(1900=0+1961=1…+1969=0}阿富汗= 100>>(1900=0+1961=100..+1969=0}

  • 国家总费用=101
  • 阿鲁巴(1900=0+1961=1…+1969=0)+ 阿富汗(1900=0+1961=100…+1969=0

    有人能帮我在MongoDb中进行上述计算吗

    然而,我已经写了一个查询,以获得年度总结

    db.MiltryExpenditure.aggregate([
    
     { $match: { "Type":"Country" } },
    
     {$group:{_id : null,
     1969: { $sum: { "$toDouble":"$1969" }}
    , _id : null,
     1960: { $sum: { 
    
    "$toDouble":"$1960" }},
    }}
    ])
    
    但是我不知道如何得到这些国家的总和,如果他们的国家能用标准化的方法得到这些国家的总和,那将是非常感激的

    请帮助…

    你可以这样试试

        db.MiltryExpenditure.aggregate(
       [{
            "$match": {
                "Type": "Country"
            }
        },
        {
            "$group": {
                "_id": null,
    
                "1969": {
                    "$sum": {
                        "$toDouble": "$1969"
                    }
                },
    
                "1960": {
                    "$sum": {
                        "$toDouble": "$1960"
                    }
                },
    
                "totalSummation": {
                    "$sum": {
                        "$add": [{
                            "$toDouble": "$1960"
                        }, {
                            "$toDouble": "$1961"
                        }]
                    }
                }
            }
        }
    ])
    
    允许您将对象转换为键和值数组。然后,您可以在该数组上应用,以仅获取表示年份的那些对。一旦数据集限制为年,您就可以运行,以便每年分别执行:

    db.collection.aggregate([
        {
            $project: {
                _id: 0,
                years: { 
                    $filter: { 
                        input: { $objectToArray: "$$ROOT" }, 
                        cond: { $and: [ { $gte: [ "$$this.k", "1900" ] }, { $lte: [ "$$this.k", "2020" ] } ] } 
                    } 
                }
            }
        },
        {
            $unwind: "$years"
        },
        {
            $group: {
                _id: "$years.k",
                total: { $sum: "$years.v" }
            }
        },
        {
            $sort: { _id: 1 }
        }
    ])
    

    按国家分组更容易,您可以运行两次(按文档汇总所有年份,然后在
    $group
    内):

    编辑:如果每个国家/地区只有一个文档,则可以缩短第二次查询(您可以删除
    $group
    ):

    请试试这个:

    db.yourCollection.aggregate([{ $match: { "Type": "Country" } },
    { $project: { _id: 0, Code: 0, IndicatorName: 0, Type: 0 } },
    {
        $addFields: {
            onlyYears: {
                $filter: {
                    input: { $objectToArray: "$$ROOT" },
                    as: "item",
                    cond: { $ne: ["$$item.k", 'Name'] }
                }
            }
        }
    }, {
        $project: {
            Name: 1, count: {
                $reduce: {
                    input: '$onlyYears',
                    initialValue: 0,
                    in: { $add: ["$$value", {"$toDouble": "$$this.v"}] }
                }
            }
        }
    },
    { $group: { _id: '', count: { $sum: '$count' }, data: { $push: '$$ROOT' } } }
    ])
    
    结果:

    /* 1 */
    {
        "_id" : "",
        "count" : 101.0,
        "data" : [ 
            {
                "Name" : "Aruba",
                "count" : 1.0
            }, 
            {
                "Name" : "Afghanistan",
                "count" : 100.0
            }
        ]
    }
    

    你是否只有那些年份或更多的随机n年数?还有多少其他字段?也许这必须或者可以很容易地用语言代码完成!!我有1900-2018年和265行(国家),我必须用mongo Commands完成这项任务。如果你有重复的文件,
    “Name”:“Afghanistan”
    “Name”:“Aruba”
    像这样吗?不,我没有任何重复项。这给了我一个错误,在元素列表后面写着“Syntax error:missing]”我的Syntax db.miltrype.aggregate([{“$match”:{“Type”:“Country”},{“$group”:{“{id”:null,“1969”:{“$sum”:{“$toDouble”:“$1969”},“1960”:{“$sum”:{“$toDouble”:“$1960”}},“总计”:{“$sum”:{“$add”:[“$toDouble”:“$1960”,“$toDouble”:“$1961”]}}}}]);现在已更正答案检查。您是否可以建议,如何从该查询中获取前10个值这给了我一个语法错误,表示$add仅支持数字或数据类型而不支持字符串,我相信此错误是因为上载到集合的CSV数据将费用值识别为字符串。您能否帮助包括将字符串转换为double aswell@Udk:更新代码w.r.t.费用字符串值!!你是说
    Name
    data
    ?你能在这里给我你的结果吗..这是我得到的结果{“\u id:”,“count”:3240672793.3,“data”:[{“Name:”0,“count”:1},{“Name:”0,“count”:3240672792.3}]@Udk:在聊天室里试试我的答案
    db.yourCollection.aggregate([{ $match: { "Type": "Country" } },
    { $project: { _id: 0, Code: 0, IndicatorName: 0, Type: 0 } },
    {
        $addFields: {
            onlyYears: {
                $filter: {
                    input: { $objectToArray: "$$ROOT" },
                    as: "item",
                    cond: { $ne: ["$$item.k", 'Name'] }
                }
            }
        }
    }, {
        $project: {
            Name: 1, count: {
                $reduce: {
                    input: '$onlyYears',
                    initialValue: 0,
                    in: { $add: ["$$value", {"$toDouble": "$$this.v"}] }
                }
            }
        }
    },
    { $group: { _id: '', count: { $sum: '$count' }, data: { $push: '$$ROOT' } } }
    ])
    
    /* 1 */
    {
        "_id" : "",
        "count" : 101.0,
        "data" : [ 
            {
                "Name" : "Aruba",
                "count" : 1.0
            }, 
            {
                "Name" : "Afghanistan",
                "count" : 100.0
            }
        ]
    }