mongodb中数组中的$ sum值

时间:2018-06-14 19:59:55

标签: mongodb mongodb-query aggregation-framework

我们有一个具有以下形式的Mongo集合:

[
    {
        "_id" : "34210db0-6g91-83e8-ae8c-659f064f503f",
        "dateReceived" : ISODate("2018-01-01T00:00:00.000Z"),
        "group" : null,
        "clientShortName" : "My Client Name",
        "sourceShortName" : "Datasource Name",
        "files" : [ 
            {
                "_id" : "807061f0-2d77-87e8-8610-9ff3cbc9c774"
                "status" : 1,
                "fileName" : "filename1.csv",
                "numRows" : 15,

            }, 
            {
                "_id" : "587036f0-2n65-55e8-8610-3ee3cbc9c814"
                "status" : 8,
                "fileName" : "filename2.csv",
                "numRows" : 30,
            }
        ]
    }
]

我们有一个使用“find”和“map”的Mongo查询,以便生成转换后的输出。 find / map命令如下所示:

db.getCollection('batches')
.find({_id: "34210db0-6g91-83e8-ae8c-659f064f503f"}, {"__v": false, "files.diffHistory": false})
.map( doc =>
    {
        doc.id = doc._id;
        doc.clientName = doc.clientShortName;
        doc.dataSourceName = doc.sourceShortName;
        delete doc._id;
        delete doc.clientShortName;
        delete doc.sourceShortName;

        doc.numFiles = NumberInt(doc.files.length);

        doc.files = doc.files.map( file =>
            {
                file.id = file._id;
                delete file._id;
                delete file.__v;
                delete file.edits;

                return file;
            }
        );

        // broken....how should this be formatted?    
        doc.totalNumRows = {$sum: doc.files.numRows};

        return doc;
    }
)

此查找/映射有效并为“totalNumRows”求和产生预期输出EXCEPT。我们要完成的是添加所有“files.numRows”字段,以便在返回的数据集的顶层获得单个汇总条目。也就是说,我们会看到一个看起来像的结果集:

[
        {
            "id" : "34210db0-6g91-83e8-ae8c-659f064f503f",
            "dateReceived" : ISODate("2018-01-01T00:00:00.000Z"),
            "group" : null,
            "clientName" : "My Client Name",
            "dataSourceName" : "Datasource Name",
            "files" : [ 
                {
                    "id" : "807061f0-2d77-87e8-8610-9ff3cbc9c774"
                    "status" : 1,
                    "fileName" : "filename1.csv",
                    "numRows" : 15,

                }, 
                {
                    "id" : "587036f0-2n65-55e8-8610-3ee3cbc9c814"
                    "status" : 8,
                    "fileName" : "filename2.csv",
                    "numRows" : 30,
                }
            ],

            "totalNumRows": 45

        }
    ]

到目前为止尝试的所有内容都会导致查询失败。有没有人知道用于总结“文件”子文档的“numRows”字段的正确命令/格式?

1 个答案:

答案 0 :(得分:1)

那么你可以做得更好更快,那么......尝试$project重命名你的实际字段名称,然后$sum来计算总数numRows

db.collection.aggregate([
  { "$match": { _id: "34210db0-6g91-83e8-ae8c-659f064f503f" }},
  { "$project": {
    "totalNumRows": {
      "$sum": "$files.numRows"
    },
    "clientName": "$clientShortName",
    "dataSourceName": "$sourceShortName",
    "files": "$files"
  }}
])

它将提供以下输出

[
  {
    "_id": 1111,
    "clientName": "My Client Name",
    "dataSourceName": "Datasource Name",
    "files": [
      {
        "fileName": "filename1.csv",
        "id": 2222,
        "numRows": 15,
        "status": 1
      },
      {
        "fileName": "filename2.csv",
        "id": 3333,
        "numRows": 30,
        "status": 8
      }
    ],
    "totalNumRows": 45
  }
]