mongodb聚合格式结果

时间:2017-06-15 14:38:55

标签: mongodb aggregation-framework

我'我试图执行以下db mongo命令:

db.getCollection('fxh').aggregate([ { $unwind : "$pf.acc"}, {$match : {"username" : "user1", "pf.acc.accnbr" : 915177}}, { $unwind : "$pf.acc.cyc"}, {$match : {"pf.acc.cyc.name": "QPT192017"}}, {$project : {_id:0, det : "$pf.acc.cyc.det"}} ])

从以下json中提取:

    "pf" : [
         { 
        "acc" : [ 
            {
                "cyc" : [ 
                    {
                        "cyc" : 1497457700,
                        "name" : "QPT192017",
                        "description" : "Quaterly Private Test",
                        "startdate" : "2017-05-10T16:28",
                        "enddate" : "2017-08-08T16:28",
                        "age" : 90,
                        "target" : 2,
                        "status" : "Closed",
                        "active" : true,
                        "det" : [ 
                            {
                                "dcycid" : 1497457701,
                                "status" : "New",
                                "age" : 0,
                                "progress" : 0,
                                "createDate" : "2017-06-14T16:28"
                            }, 
                            {
                                "dcycid" : 12345678.0,
                                "status" : "Start",
                                "age" : 10.0,
                                "progress" : 0.0,
                            }, 
                            {
                                "dcycid" : 12345679.0,
                                "status" : "Running",
                                "age" : 100.0,
                                "progress" : 5.0,

                            }, 
                            {
                                "dcycid" : 12345680.0,
                                "status" : "Running",
                                "age" : 500.0,
                                "progress" : 15.0,

                            }, 
                            {
                                "dcycid" : 12345681.0,
                                "status" : "Running",
                                "age" : 1500.0,
                                "progress" : 35.0,

                            }, 
                            {
                                "dcycid" : 12345682.0,
                                "status" : "Running",
                                "age" : 51500.0,
                                "progress" : 85.0,

                            }, 
                            {
                                "dcycid" : 12345683.0,
                                "status" : "Running",
                                "age" : 58500.0,
                                "progress" : 103.0,

                            }, 
                            {
                                "dcycid" : 12345684.0,
                                "status" : "Closed",
                                "age" : 58500.0,
                                "progress" : 103.0,

                            }
                        ]
                    }
                ]
            }
        ],
        "active" : true,
        "pfid" : 1496513935
    }

该命令的结果如下:

   [
    {
        "det" : [ 
            {
                "dcycid" : 1497457701,
                "status" : "New",
                "age" : 0,
                "progress" : 0,
                "createDate" : "2017-06-14T16:28"
            }, 
            {
                "dcycid" : 12345678.0,
                "status" : "Start",
                "age" : 10.0,
                "progress" : 0.0,
            }, 
            {
                "dcycid" : 12345679.0,
                "status" : "Running",
                "age" : 100.0,
                "progress" : 5.0,
            }, 
            {
                "dcycid" : 12345680.0,
                "status" : "Running",
                "age" : 500.0,
                "progress" : 15.0,
            }, 
            {
                "dcycid" : 12345681.0,
                "status" : "Running",
                "age" : 1500.0,
                "progress" : 35.0,
            }, 
            {
                "dcycid" : 12345682.0,
                "status" : "Running",
                "age" : 51500.0,
                "progress" : 85.0,
            }, 
            {
                "dcycid" : 12345683.0,
                "status" : "Running",
                "age" : 58500.0,
                "progress" : 103.0,
            }, 
            {
                "dcycid" : 12345684.0,
                "status" : "Closed",
                "age" : 58500.0,
                "progress" : 103.0,
            }
        ]
    }
]

这是我的问题,我只需要获得' det'部分

"det" : [
{.....},
{.....},
{.....}
]

知道如何调整我的mongo命令以获得所需的结果吗?

1 个答案:

答案 0 :(得分:0)

初始查询是:

db.getCollection('fxh').aggregate([ { $unwind : "$pf.acc"}, {$match : {"username" : "user1", "pf.acc.accnbr" : 915177}}, { $unwind : "$pf.acc.cyc"}, {$match : {"pf.acc.cyc.name": "QPT192017"}}, {$project : {_id:0, det : "$pf.acc.cyc.det"}} ])
'det'投影本身就是一个数组,而不是通过指定对象名来运行以下内容来给出我正在寻找更多/更少的内容:

例如:db.getCollection('fxh').aggregate([ { $unwind : "$pf.acc"}, {$match : {"username" : "user1", "pf.acc.accnbr" : 915177}}, { $unwind : "$pf.acc.cyc"}, {$match : {"pf.acc.cyc.name": "QPT192017"}}, {$project : {_id:0, det : "$pf.acc.cyc.det.dcycid"}} ])