2016-05-05 30 views
5

Desidero creare aggregazione reattiva meteorica per la raccolta di Transazioni.Meteor ReactiveAggregate

Le transazioni hanno data, quindi voglio aggregare i dati per mese.

Il codice è:

ReactiveAggregate(this, Transactions, [ 
    { 
     $match: { 
     'date': { 
      $gte: new Date(startDate), 
      $lt: new Date(endDate) 
     } 
     } 
    }, 
    { 
     '$group' : 
     { 
     '_id' : { month: { $month: "$date" }}, 
     'totalProfit': { $sum: "$totalProfit"}, 
     'totalSales': { $sum: "$totalSales" }, 
     'totalExpenses': { $sum: "$totalExpenses" }, 
     count: { $sum: 1 } 
     } 
    }, 
    { 
     '$project':{ 
     date: '$date', 
     totalProfit: '$totalProfit', 
     totalSales: '$totalSales', 
     totalExpenses: '$totalExpenses', 
     } 
    } 
    ], { clientCollection: "report3MonthsTransactions" }); 

}); 

Quando faccio questo, vi verrà chiesto di errore:

Error: Meteor does not currently support objects other than ObjectID as ids

Grazie!

+0

hai provato a utilizzare semplicemente '_id: {$ mese:" $ date "}'? – MasterAM

risposta

2

tuo clausola $group è:

'$group' : { 
    '_id' : { month: { $month: "$date" }}, 
    ... 
} 

che risulta in ogni documento avente un composito _id: {_id: {month: <someMonth>}, ...}, dove ogni _id è un oggetto che non è effettivamente un ObjectID.

Nel tuo caso, avere {_id: <someMonth>, ...} sarebbe sufficiente.

Ciò può essere ottenuto mediante raggruppamento come segue:

'$group' : { 
    '_id' : { $month: "$date" }, 
    ... 
} 

A proposito, se avete bisogno di avere la _id come una stringa (e penso che si fa), è possibile utilizzare $substr per convertirlo :

'$group' : { 
    _id: {$substr: [{$month: '$date'}, 0, 2]}, 
    ... 
} 
+0

Grazie. Funziona!! –