[MongoDB & Mongoose] How to use a variable value as a field in aggregation query ($group)?

Hi all, I would like to define modifierName from email variable and use it as a field in an aggregation query (specifically $group). How can I achieve that with this code?

let data = await Request.aggregate([
  { $match: searchFilters },
  { $lookup: {
    from: 'user',
    localField: 'user',
    foreignField: '_id',
    as: 'modifier',
  } },
]);

let email = data.email; // julio@email.com
let modifierName = email.split('@')[0]; // julio

data = await Request.aggregate([
  { $match: searchFilters },
  { $lookup: {
    from: 'user',
    localField: 'user',
    foreignField: '_id',
    as: 'modifier',
  } },
  { $group: {
    _id: {
      date: groupIdDate,
    },
    modifierName: { $sum: 1 }
  } }
]);

Here is this question in a picture :

Any suggestion is appreciated, thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.