Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-28954

Multiple out stages in aggregate

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None
    • Query Optimization

      I would like to be able to use the $out pipeline embedded into the aggregation in multiple locations to snapshot the current output.

      Example
      [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.

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            paul.reed Paul Reed
            Votes:
            0 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated: