Mongoose sum aggregation -
i have mongoose schema sum of sizes based on creator.
creator: { type: schema.types.objectid, ref: 'user' }, archives: [{ archiveid: string, url: string, name: string, size: number, isset: { type: boolean, default: false }, timestamp: { type: date, default: date.now() } }]
this have tried far, total keeps coming 0
var objectid = require('mongoose').types.objectid; archivemodel.aggregate( { $match: { 'creator': new objectid(creatorid), } }, { $group: { _id: null, total: {$sum: '$archives.size'} } }, { $project: { creator: 1, total: 1 } }, function(err, result) { console.log(err); console.log(result); } );
db.col.aggregate([ // match docs right creator {$match: {creator: my_creator}}, // array of arrays single array {$unwind: '$archives'}, // count of docs {$group: {_id: 'total', count: {$sum: 1}}} ]);
Comments
Post a Comment