Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb聚合json格式按月结果_Mongodb_Aggregation Framework - Fatal编程技术网

Mongodb聚合json格式按月结果

Mongodb聚合json格式按月结果,mongodb,aggregation-framework,Mongodb,Aggregation Framework,您好,我正在使用将在highcharts中使用的reporting api,我是mongodb的新手 以下是我的汇总查询(建议我修改): 输出: { "_id" : { "year" : NumberInt(2019), "month" : NumberInt(2), "productid" : "11" }, "totalQty" : 6.0 } // -------------------------------

您好,我正在使用将在highcharts中使用的reporting api,我是mongodb的新手

以下是我的汇总查询(建议我修改):

输出:

{ 
    "_id" : {
        "year" : NumberInt(2019), 
        "month" : NumberInt(2), 
        "productid" : "11"
    }, 
    "totalQty" : 6.0
}
// ----------------------------------------------
{ 
    "_id" : {
        "year" : NumberInt(2019), 
        "month" : NumberInt(2), 
        "productid" : "14"
    }, 
    "totalQty" : 7.0
}
// ----------------------------------------------
{ 
    "_id" : {
        "year" : NumberInt(2019), 
        "month" : NumberInt(1), 
        "productid" : "13"
    }, 
    "totalQty" : 3.0
}
// ----------------------------------------------
{ 
    "_id" : {
        "year" : NumberInt(2019), 
        "month" : NumberInt(2), 
        "productid" : "10"
    }, 
    "totalQty" : 6.0
}
// ----------------------------------------------
{ 
    "_id" : {
        "year" : NumberInt(2018), 
        "month" : NumberInt(2), 
        "productid" : "12"
    }, 
    "totalQty" : 5.0
}
// ----------------------------------------------
{ 
    "_id" : {
        "year" : NumberInt(2019), 
        "month" : NumberInt(2), 
        "productid" : "15"
    }, 
    "totalQty" : 8.0
}
// ----------------------------------------------
{ 
    "_id" : {
        "year" : NumberInt(2019), 
        "month" : NumberInt(1), 
        "productid" : "11"
    }, 
    "totalQty" : 2.0
}
// ----------------------------------------------
我希望在输出中包含以下内容:

status: 200,
msg: "SUCCESS"
data: [{
    1:[
        {
            "productid": 11,
            "totalQty": 3
        },
        {
            "productid": 12,
            "totalQty": 27
        }
    ],

    2:[
        {
            "productid": 11,
            "totalQty": 64
        },
        {
            "productid": 12,
            "totalQty": 10
        }
    ]   
}]
附加收藏的图像以供参考


是否有任何方法可以使用聚合或其他方法实现它,否则我将不得不通过代码手动完成?

您可以将以下聚合阶段附加到当前管道中:

db.product_sold.aggregate([
    // your current $group stage
    {
        $group: {
            _id: "$_id.month",
            docs: { $push: { productid: "$_id.productid", totalQty: "$totalQty" } }
        }
    },
    {
        $project: {
            _id: 0,
            k: { $toString: "$_id" },
            v: "$docs"
        }
    },
    {
        $group: {
            _id: null,
            data: { $push: "$$ROOT" }
        }
    },
    {
        $project: {
            _id: 0,
            data: { $arrayToObject: "$data" }
        }
    }
])

这里的想法是,您可以使用将
\u id
设置为
null
将所有数据放入单个文档中,然后使用将月数作为键,将该月的所有聚合作为值。

try@SagarVirpara这是因为您在4.0以下使用MongoDB,您可以使用
k:{$substr:[“$\u id”,0,-1]}
作为替代
k:{$toString:$\u id}
好的解决方法。。如果我想获得2019年等给定年份的数据,该怎么办?我使用的是mongodb 3.2版,因此我也无法使用$ArrayObject。@SagarVirpara有没有升级mongodb的可能性?恐怕这是动态生成密钥的唯一方法
db.product_sold.aggregate([
    // your current $group stage
    {
        $group: {
            _id: "$_id.month",
            docs: { $push: { productid: "$_id.productid", totalQty: "$totalQty" } }
        }
    },
    {
        $project: {
            _id: 0,
            k: { $toString: "$_id" },
            v: "$docs"
        }
    },
    {
        $group: {
            _id: null,
            data: { $push: "$$ROOT" }
        }
    },
    {
        $project: {
            _id: 0,
            data: { $arrayToObject: "$data" }
        }
    }
])