I have an array called group containing a list of IDs, I have the find and populate statement below which works perfectly with this and I use populate to get the additional data - query below:
var stories = await Story .find( { 'group' : { $in : group } } ) .populate('group') .populate('createdBy') .sort('-created') I have an aggregate query (below) which does what I want but 1) it does not use the values in the group array, it simply returns all of the content, and 2) I don't get the additional data for the group and createdBy fields as I do for the find above.
var spellings = await Spelling.aggregate([ { "$sort": { "keyStageLevel": 1, "spellingLevel": 1 } }, { "$group" : { "_id": { "keyStageLevel": "$keyStageLevel", "class": "$class" }, "data": { "$push": { "spellingLevel": "$spellingLevel", "class": "$class", "name": "$name", "spellings": "$spellings", "created": "$created", "_id": "$_id", "group": "$group", "createdBy": "$createdBy" } } }}, { "$sort": { "_id": 1 } } ]) Edit - here is an example spelling document:
{ "_id":"5b1ff6f62bb1894efcf76ea0", "spellings":["here","are","spellings"], "name":"withclass", "group":"5ab79a639083cf35339b880a", "createdBy":"5ab79185d47b833506ff94b1", "created":"2018-06-12T16:38:14.571Z", } Can anyone help with how to use the values in the group array in my aggregate statement and how I can add in the additional data for the group and createdBy fields as I do for the find ?
$lookupaggregation stage which easily populate yourgroupandcreatedBy... which mongodb version do you have?