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:
- split the “code” value into two piece of data (A=superset, 1=index)
- 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?