2016-03-02 67 views

回答

0

请尽量通过做aggregation

> db.collection.aggregate([ 
      // 1. unwind the array a with this array index 
      {$unwind: {path: '$a', includeArrayIndex: 'index'}}, 
      // 2. sum them by their index 
      {$group: { 
        _id: '$index', 
        a: {$sum: '$a'} 
      }}, 
      // 3. sort by array index 
      {$sort: {_id: 1}}, 
      // 4. group them into the sum array 
      {$group: {_id: null, sum: {$push: '$a'}}} 
    ])