使用mongo模板聚合框架转换mongodb查询

时间:2017-10-05 14:03:15

标签: mongodb spring-data-mongodb

以下是涉及正在使用的聚合的查询:

db.image.aggregate([
    {
        $group: { 
            _id: '$cameraId' , captured: {$sum: 1},
            singleFrameProcessed: {$sum: {$cond: ["$singleFrameProcessed", 1, 0]}},
            batchProcessed: {$sum: {$cond: ["$batchProcessed", 1, 0]}},
        }
    },
    {$project: {_id:0, cameraSource: "$_id", captured: 1, singleFrameProcessed: 1, batchProcessed: 1}}
])

问题是无法使用mongo template转换为aggregation表单。有条件的问题。

我的输入(数百份文件)1:

{
    "_id" : ObjectId("59d29437e38cbf46b44f2e9f"),
    "guid" : "82057627-c4fd-4c10-9d43-f897491a3dc7",
    "filepath" : "FW-MD-1234",
    "insertTime" : NumberLong(1506972726974),
    "cameraId" : "1234",
    "computerId" : "FW-MD-PacoWork",
    "capturetime" : 1234,
    "iso" : 1234,
    "shutterspeed" : 1234.0,
    "aperature" : 1234.0,
    "singleFrameProcessTime" : 0,
    "singleFrameProcessor" : "",
    "singleFrameCollectionSetGuid" : "",
    "batchProcessTime" : 0,
    "batchProcessor" : "",
    "batchCollectionSetGuid" : "",
    "singleFrameProcessed" : false,
    "batchProcessed" : false
}

我的输出是每个摄像头的摘要:

{
    "captured" : 10.0,
    "singleFrameProcessed" : 4.0,
    "batchProcessed" : 3.0,
    "cameraSource" : "1234"
}    
/* 2 */
{
    "captured" : 11.0,
    "singleFrameProcessed" : 3.0,
    "batchProcessed" : 0.0,
    "cameraSource" : "052052001324"
}    
/* 3 */
{
    "captured" : 68527.0,
    "singleFrameProcessed" : 66297.0,
    "batchProcessed" : 925.0,
    "cameraSource" : "052052001304"
}

0 个答案:

没有答案