How to replace existing object fields with a new array field

I’m working within an aggregation pipeline (as there are various other things I need to do). I have a part of my document that starts off like…

abcd: {
  code: "A1",
  weekOne: ObjectID(),
  weekTwo: ObjectID(),
  weekThree: ObjectID(),
  weekFour: ObjectID(),
}

I use the following $lookup …

  {
    $lookup: {
      from: "programmings",
      localField: "weekOne",
      foreignField: "_id",
      pipeline: [
          {
            $project: {
              _id: 0,
              sets: 1,
              setRest: 1,
              reps: 1,
              repRest: 1
            },
          }
      ],
      as: "weekOne",
    }
  },
  { $unwind: "$weekOne" },

to give me something that looks like this…

abcd: {
  code: "A1",
  weekOne: { sets, setRest, reps, repRest },
  weekTwo: { sets, setRest, reps, repRest },
  weekThree: { sets, setRest, reps, repRest },
  weekFour: { sets, setRest, reps, repRest },
}

which is close, but I want to have:

  1. split the “code” value into two piece of data (A=superset, 1=index)
  2. put the contents of weekOne, weekTwo, etc as elements of an array
    so that it would look like so…
abcd: {
  superset: "A", 
  supersetIndex: 1,
  valuesThatVaryByWeek: [
    0: { week: 1,
         sets, setRest, reps, repRest },
    1: {week: 2,
         sets, setRest, reps, repRest },
    2: {week: 3,
         sets, setRest, reps, repRest },
    3: {week: 4,
         sets, setRest, reps, repRest }
  ]
}

How do I achieve this?

Okay, so, turns out writing the question out for y’all helped me change how I think about it, and get an answer. Typical :slight_smile:

For adding the week number, I put that into an $addFields step, immediately after the relevant $project, e.g. in the $lookup snippet I showed above, it became…

  {
    $lookup: {
        from: "programmings",
        localField: "weekOne",
        foreignField: "_id",
        pipeline: [
          {
            $project: {
              _id: 0,
              sets: 1,
              setRest: 1,
              reps: 1,
              repRest: 1
            },
          },
          {
            $addFields: {
              week: 1
            }
          }
        ],
        as: "weekOne",
    }
  },
  { $unwind: "$weekOne" },
  ...move onto $lookup for weekTwo, etc...
  ..finish with a $project step to get these into the parent object

Then for the splitting of the code field to make new fields superset and supersetIndex I used a $substr and added new fields to my final $project step. That made me realise I could also do the same for getting the weekOne etc to be a happy array. So my final $project step now looks like…

  {
      $project: {
        _id:0,
        superset: { $substr: [ "$code", 0, 1] },
        supersetIndex: { $substr: [ "$code", 1, 1] },
        exercise: "$exercise",
        valuesThatVaryByWeek: [
          "$weekOne",
          "$weekTwo",
          "$weekThree",
          "$weekFour"
          ]
      }

Thanks for listening ether, and I’ll leave this here in case it helps anyone else :slight_smile:

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