I have a collection where users periodically enter a “profile entry”. Each entry stores (among other things) a unique id associated with an organization (‘orgId’) and the date of the entry (‘date’). I do this so I can capture changes in different numbers over time (a pseudo example of schema is below).
At some point I need to grab the latest entry from each user ( or ‘orgId’ in the schema below). Then I need to sum the employees for each organization.
I can’t seem to figure this out. I tried the following but it’s just returning all the employee values into the MostRecentImpactEntriesArray array.
var yourClients = // this is an array of orgIds
var pipeline = [
{$match: {orgId: {$in: yourClients}}},
{$group: {_id: {orgId: '$orgId', FT: '$employment.Employees'}, date: {$last: '$date'}}}
];
var MostRecentImpactEntriesArray =
ImpactProfile.aggregate(pipeline).map(function(doc){
return doc._id.FT
});
`
Schemas = {};
Schemas.Employment = new SimpleSchema({
Employees: {
type: Number,
optional: false,
label: '# of Employees:',
}
});
Schemas.ProfileEntry = new SimpleSchema({
orgId: {
type: String,
optional: false,
},
date: {
type: Date,
optional: false,
label: 'Calendar Year for this Roster:',
autoform: {
type: "pickadate",
}
},
employment: {
type: Schemas.Employment,
label: "Employement Impact History:",
optional: false,
},
});
ProfileEntry = new Mongo.Collection("ProfileEntry");
ProfileEntry.attachSchema(Schemas.ProfileEntry);
ProfileEntry.allow({
insert: function () { return true; },
update: function () { return true; },
remove: function () { return true; }
});
`