我有数据集,我需要来自该数据集的两个不同的group by值。请找到下面的数据集,
[{
"ASSIGN_ID": "583f84bce58725f76b322398",
"SPEC_ID": "58411772",
"STATUS": 1,
"UPDATE_DATE": ISODate("2016-12-21T04:10:23.000Z")
},
{
"ASSIGN_ID": "583f84bce58725f76b322398",
"SPEC_ID": "58411772",
"STATUS": 4,
"UPDATE_DATE": ISODate("2016-12-22T04:10:23.000Z")
},
{
"ASSIGN_ID": "583f84bce58725f76b322398",
"SPEC_ID": "58411772",
"STATUS": 4,
"UPDATE_DATE": ISODate("2016-12-23T04:10:23.000Z")
},
{
"ASSIGN_ID": "583f84bce58725f76b322398",
"SPEC_ID": "58411774",
"STATUS": 3,
"UPDATE_DATE": ISODate("2016-12-24T04:10:23.000Z")
},
{
"ASSIGN_ID": "583f84bce58725f76b322311",
"SPEC_ID": "58411775",
"STATUS": 1,
"UPDATE_DATE": ISODate("2016-12-25T04:10:23.000Z")
},
{
"ASSIGN_ID": "583f84bce58725f76b322311",
"SPEC_ID": "58411775",
"STATUS": 3,
"UPDATE_DATE": ISODate("2016-12-23T04:10:23.000Z")
},
{
"ASSIGN_ID": "583f84bce58725f76b322322",
"SPEC_ID": "58411774",
"STATUS": 1,
"UPDATE_DATE": ISODate("2016-12-20T04:10:23.000Z")
},
{
"ASSIGN_ID": "583f84bce58725f76b322322",
"SPEC_ID": "58411778",
"STATUS": 4,
"UPDATE_DATE": ISODate("2016-12-21T04:10:23.000Z")
}
]
我想使用 ASSIGN_ID 和状态对此进行分组,并且还要在每个 ASSIGN_ID 组内进行分组 SPEC_ID 以及状态。请在下面找到预期的输出,
[{
"ASSIGN_ID": "583f84bce58725f76b322398",
"ASSIGN_GROUP": [{
"STATUS": 1,
"COUNT": 1
},
{
"STATUS": 3,
"COUNT": 1
},
{
"STATUS": 4,
"COUNT": 2
}
],
"SPEC_ARRAY": [{
"SPEC_ID": "58411772",
"SPEC_GROUP": [{
"STATUS": 1,
"COUNT": 1
},
{
"STATUS": 4,
"COUNT": 2
}
]
},
{
"SPEC_ID": "58411774",
"SPEC_GROUP": [{
"STATUS": 3,
"COUNT": 1
}]
}
]
},
{
"ASSIGN_ID": "583f84bce58725f76b322311",
"ASSIGN_GROUP": [{
"STATUS": 1,
"COUNT": 1
},
{
"STATUS": 3,
"COUNT": 1
}
],
"SPEC_ARRAY": [{
"SPEC_ID": "58411775",
"SPEC_GROUP": [{
"STATUS": 1,
"COUNT": 1
},
{
"STATUS": 3,
"COUNT": 1
}
]
}]
},
{
"ASSIGN_ID": "583f84bce58725f76b322322",
"ASSIGN_GROUP": [{
"STATUS": 1,
"COUNT": 1
},
{
"STATUS": 4,
"COUNT": 1
}
],
"SPEC_ARRAY": [{
"SPEC_ID": "58411774",
"SPEC_GROUP": [{
"STATUS": 1,
"COUNT": 1
}]
},
{
"SPEC_ID": "58411778",
"SPEC_GROUP": [{
"STATUS": 4,
"COUNT": 1
}]
}
]
}
]
目前,我到目前为止所做的只是 ASSIGN_ID 和状态的分组,请找到以下代码,
Modal.aggregate([
{
"$group": {
"_id": {
"INSPECTED_BY": "$INSPECTED_BY",
"STATUS": "$STATUS"
},
"spec_id": "$SPEC_ID",
"total": {
"$sum": 1
}
}
}, {
"$group": {
"_id": "$_id.INSPECTED_BY",
"data": {
"$push": {
"STATUS": "$_id.STATUS",
"total": "$total"
}
}
}
}
]);
,结果是
[
{
"_id": "583f84bce58725f76b322398",
"data": [
{
"STATUS": 1,
"COUNT": 1
}, {
"STATUS": 3,
"COUNT": 1
}, {
"STATUS": 4,
"COUNT": 2
}
]
}, {
"_id": "583f84bce58725f76b322311",
"data": [
{
"STATUS": 1,
"COUNT": 1
}, {
"STATUS": 3,
"COUNT": 1
}
]
}, {
"_id": "583f84bce58725f76b322322",
"data": [
{
"STATUS": 1,
"COUNT": 1
}, {
"STATUS": 4,
"COUNT": 1
}
]
}
]
请给我一个合适的解决方案,以获得预期的结果。
答案 0 :(得分:1)
您可以尝试以下版本3.2的替代聚合。
Modal.aggregate([{
$group: {
_id: {
ASSIGN_ID: "$ASSIGN_ID",
SPEC_ID: "$SPEC_ID",
STATUS: "$STATUS"
},
COUNT: {
$sum: 1
}
}
}, {
$group: {
_id: {
ASSIGN_ID: "$_id.ASSIGN_ID",
SPEC_ID: "$_id.SPEC_ID"
},
SPEC_GROUP: {
$push: {
STATUS: "$_id.STATUS",
COUNT: "$COUNT"
}
}
}
}, {
$group: {
_id: "$_id.ASSIGN_ID",
SPEC_ARRAY: {
$push: {
SPEC_ID: "$_id.SPEC_ID",
SPEC_GROUP: "$SPEC_GROUP"
}
}
}
}, {
$project: {
ASSIGN_ID: "$_id",
ASSIGN_GROUP: "$SPEC_ARRAY.SPEC_GROUP",
SPEC_ARRAY: 1
}
}, {
$unwind: "$ASSIGN_GROUP"
}, {
$unwind: "$ASSIGN_GROUP"
}, {
$group: {
_id: "$ASSIGN_ID",
ASSIGN_GROUP: {
$push: "$ASSIGN_GROUP"
},
SPEC_ARRAY: {
$first: "$SPEC_ARRAY"
}
}
}])
将最后四个阶段替换为版本3.4的$reduce
{
$project: {
_id: 0,
ASSIGN_ID: "$_id",
SPEC_ARRAY: 1,
ASSIGN_GROUP: {
$reduce: {
input: "$SPEC_ARRAY.SPEC_GROUP",
initialValue: [],
in: {
$concatArrays: ["$$value", "$$this"]
}
}
}
}
}
答案 1 :(得分:0)
它是一个多通道管道;如果需要在同一通道内获取两组不同的分组;必须首先分组一组。一旦到达该集合,请保留下一组分组所需的数据。
涉及的管道:
group
:( 1)按ASSIGN_ID,SPEC_ID,STATUS分组 - 获取此组合的计数
group
:( 2)按ASSIGN_ID分组,SPEC_ID - 准备SPEC_GROUP阵列
group
:( 3)按ASSIGN_ID分组 - 准备SPEC_ARRAY对象数组
project
:选择ASSIGN_ID,SPEC_ARRAY,SPEC_ARRAY数据的副本,用于按状态准备分组
unwind
:展开SPEC Array元素以获取每个spec_id分开的数据
unwind
:展开SPEC阵列。 SPEC_GROUP元素用于获取每个spec_id / status分隔的数据
group
:( 4)按ASSIGN_ID分组,状态 - 获取每个状态的计数[获取计数ADD计数对应于自我们已按SPEC_ID分组的每个状态],从第一行中选择SPEC_ARRAY,因为它重复所有
group
:(5)Group By ASSIGN_ID - 获取赋值组数组,从第一行中选择SPEC_ARRAY,因为它重复所有
db.Modal.aggregate([ {
$group : {
_id : {
ASSIGN_ID : "$ASSIGN_ID",
SPEC_ID : "$SPEC_ID",
STATUS : "$STATUS"
},
a_s_cnt : {
$sum : 1
}
}
}, {
$group : {
_id : {
ASSIGN_ID : "$_id.ASSIGN_ID",
SPEC_ID : "$_id.SPEC_ID"
},
SPEC_GROUP : {
$push : {
STATUS : "$_id.STATUS",
COUNT : "$a_s_cnt"
}
}
}
}, {
$group : {
_id : "$_id.ASSIGN_ID",
SPEC_ARRAY : {
$push : {
SPEC_ID : "$_id.SPEC_ID",
SPEC_GROUP : "$SPEC_GROUP"
}
}
}
}, {
$project : {
_id : 0,
ASSIGN_ID : "$_id",
SPEC_ARRAY : "$SPEC_ARRAY",
forStatus : "$SPEC_ARRAY"
}
}, {
$unwind : "$forStatus"
}, {
$unwind : "$forStatus.SPEC_GROUP"
}, {
$group : {
_id : {
ASSIGN_ID : "$ASSIGN_ID",
STATUS : "$forStatus.SPEC_GROUP.STATUS"
},
statusCount : {
$sum : "$forStatus.SPEC_GROUP.COUNT"
},
SPEC_ARRAY : {
$first : "$SPEC_ARRAY"
}
}
}, {
$group : {
_id : "$_id.ASSIGN_ID",
ASSIGN_GROUP : {
$push : {
STATUS : "$_id.STATUS",
COUNT : "$statusCount"
}
},
SPEC_ARRAY : {
$first : "$SPEC_ARRAY"
}
}
}, {
$project : {
_id : 0,
ASSIGN_ID : "$_id",
ASSIGN_GROUP : "$ASSIGN_GROUP",
SPEC_ARRAY : "$SPEC_ARRAY"
}
} ])
答案 2 :(得分:0)
使用此聚合命令,使用您的样本数据进行测试
db.test.aggregate([{
$group: {
_id: {
ASSIGN_ID: "$ASSIGN_ID",
STATUS: "$STATUS",
SPEC_ID: "$SPEC_ID"
},
count: {
"$sum": 1
}
}
}, {
$group: {
_id: "$_id.ASSIGN_ID",
ASSIGN_GROUP: {
$push: {
STATUS: "$_id.STATUS",
count: "$count"
}
},
SPEC_ARRAY: {
$push: {
SPEC_ID: "$_id.SPEC_ID",
STATUS: "$_id.STATUS",
count: "$count"
}
}
}
}, {
$unwind: "$SPEC_ARRAY"
}, {
$group: {
_id: {
ASSIGN_ID: "$_id",
SPEC_ID: "$SPEC_ARRAY.SPEC_ID"
},
ASSIGN_GROUP: {
$first: "$ASSIGN_GROUP"
},
SPEC_GROUP: {
$push: {
"STATUS": "$SPEC_ARRAY.STATUS",
count: "$SPEC_ARRAY.count"
}
}
}
}, {
$group: {
_id: "$_id.ASSIGN_ID",
ASSIGN_GROUP: {
$first: "$ASSIGN_GROUP"
},
SPEC_ARRAY: {
$push: {
SPEC_ID: "$_id.SPEC_ID",
SPEC_GROUP: "$SPEC_GROUP"
}
}
}
}
]).pretty()