I'm developing a personalized digital Advent Calendar for a close friend. Each day, he gets to unlock and claim a new Steam game.
Within the user account that I created for him in mongodb, there exists a key named "codes" (object) structured as shown below:
_id: blbalbalba
codes: {
1 : {
title: GTA V,
code: AISHD-SDAH-HAUd,
claimed_at: ,
},
2 : {
title: Fortnite,
code: HHF7-d88a-88fa,
claimed_at: ,
}
}
The above is just sample data. When door number 7 is opened by the user in the client application, I aim to update the "claimed_at" key with the current date in the object corresponding to key name "7".
I experimented with various approaches like:
const result = await PrivateUserData.updateOne(
{ id: myID },
{ $set: { "codes.`${door_number}`.date_claimed" : date,
}
}
);
However, none of these attempts were successful. What did work was using a static path like "codes.5.date_claimed". This way, the object with name 5 successfully had its date_claimed key updated. Now, my question is, how do I implement a dynamic path utilizing a variable instead of a fixed number?
Thank you for your help!