Currently, I'm facing a challenge while developing a statistics dashboard for a meditation app. I'm struggling with creating a MongoDB query to fetch the most popular meditations based on user progress. The key collections involved are users
and meditations
.
The structure of a user document in the users
collection is as follows:
{
"_id":{
"$oid":"627b519f73b2bd3375f5a7a5"
},
"userProgress":{
"meditationsPracticed":[
{
"timestamp":"11.5.2022, 9:03:34",
"id":"5ef2ff0af1a23752be00651f"
},
{
"timestamp":"11.5.2022, 12:46:03",
"id":"5eca520c10fe0480d350c9a4"
},
/* additional meditation entries */
]
}
}
On the other hand, a meditation document in the meditations
collection is structured like this:
{
"_id":{
"$oid":"5eca520c10fe0480d350c9ac"
},
"name":"Sleep Well",
"duration":{
"$numberInt":"250"
}
}
My aim is to devise a query that retrieves the most popular meditations based on the id
values stored in the meditationsPracticed
array within the userProgress
field of the user document.
Any assistance in crafting this MongoDB query would be highly appreciated. Thank you!
I've experimented with multiple queries utilizing $unwind and $lookup but haven't achieved success