|
I would like to be able to use the $out pipeline embedded into the aggregation in multiple locations to snapshot the current output.
[stock] is made up like:
|
{ _id:1, type:'widgets', material:'metal', name:'item1', count:5 }
|
{ _id:2, type:'widgets', material:'metal', name:'item2', count:3 }
|
{ _id:3, type:'widgets', material:'wood', name:'item3', count:2 }
|
{ _id:4, type:'widgets', material:'wood', name:'item4', count:5 }
|
{ _id:5, type:'widgets', material:'plastic',name:'item5', count:6 }
|
{ _id:6, type:'casing', material:'metal', name:'item6', count:0 }
|
{ _id:7, type:'casing', material:'wood', name:'item7', count:1 }
|
{ _id:8, type:'casing', material:'plastic', name:'item8', count:9 }
|
{ _id:9, type:'casing', material:'plastic', name:'item9', count:2 }
|
|
db.stock.aggregate([
|
{ $group:{ _id: { k1:'$type', k2:'$material' }, totalcount:{$sum:'$count'} } },
|
{ $out:'group_type_material' },
|
{ $group:{ _id: '$_id.k1', totalcount:{$sum:'$totalcount'} } },
|
{ $out:'group_type' },
|
{ $group:{ _id: null, totalcount:{$sum:'$totalcount'} } },
|
{ $out:'group_all' },
|
])
|
which would result in collections of
[group_type_material]:
|
|
{ _id: { k1:'widgets', k2:'metal'}, totalcount:8 }
|
{ _id: { k1:'widgets', k2:'wood'}, totalcount:7 }
|
{ _id: { k1:'widgets', k2:'plastic'}, totalcount:6 }
|
{ _id: { k1:'casing', k2:'metal'}, totalcount:0 }
|
{ _id: { k1:'casing', k2:'wood'}, totalcount:1 }
|
{ _id: { k1:'casing', k2:'plastic'}, totalcount:11 }
|
|
|
[group_type]:
|
|
{ _id: 'widgets', totalcount:21 }
|
{ _id: 'casing', totalcount:12 }
|
|
|
[group_all]:
|
|
{ _id: null, totalcount:33 }
|
This would make this aggregation truly powerful in my opinion.
|