After thorough research, I have not been able to find a precise solution to my issue. Despite going through the Mongoose documentation and attempting various approaches, as a newcomer to MongoDB, I am eager to create my own API following numerous tutorials.
The Objective:
To execute a GET request to
and retrieve the details of series http://localhost:3000/seasons/19/1
19
along with the selected episode 1
Something similar to:
{
"_id": "5b893aff76c12c166c4e75ae",
"season_number": 19,
"name": "Season 19",
"number_of_episodes": 24,
"episode": [
{
"_id": "5b895e56089a9a152c16af1f",
"episode_number": 1,
"name": "19.01",
"length": "1:23:33",
"author": "Aaron Wright",
"release": "2018-01-13T00:00:00.000Z",
"description": "We kick off our 2018 season with the worst of CES and the future of smart toilets before Aaron unravels the controversial “Dear David” saga that has sparked controversy. We then look at state sponsored mind control programs and the hypnotic experiments performed on unwitting Princeton students in the 1960s."
},
}
The Challenge:
I am struggling to devise a method to return the object as shown above with just the single selected episode. While all the data is present in episodes
, I can fetch it by manually using episodes[0]
etc.
For those interested, here is the repository file to avoid prolonging this post: https://github.com/DannnB/mysterious-universe-api/blob/master/api/controllers/seasons.js
- Line:
63
- Name:
seasons_get_episode_number
Appreciate any assistance provided!
The database - hosted on MongoDB Atlas - operating on the free tier thus unable to utilize $where
and opting against its usage due to its resource-intensive nature.
"data": [
{
"_id": "5b893aff76c12c166c4e75ae",
"season_number": 19,
"name": "Season 19",
"number_of_episodes": 24,
"episodes": [
{
"_id": "5b895e56089a9a152c16af1f",
"episode_number": 1,
"name": "19.01",
"length": "1:23:33",
"author": "Aaron Wright",
"release": "2018-01-13T00:00:00.000Z",
"description": "We kick off our 2018 season with the worst of CES and the future of smart toilets before Aaron unravels the controversial “Dear David” saga that has sparked controversy. We then look at state sponsored mind control programs and the hypnotic experiments performed on unwitting Princeton students in the 1960s."
},
{
"_id": "5b895fee089a9a152c16af20",
"episode_number": 2,
"name": "19.02",
"length": "1:22:11",
"author": "Benjamin Grundy",
"release": "2018-01-20T00:00:00.000Z",
"description": "This week we unravel the cosmic serpent to find the origins of life and the link between DNA and Ayahuasca visions. Could the building blocks of all life also be a conscious force that is capable of direct communication with our altered states of mind?"
}
]
},
{
"_id": "5b893b2276c12c166c4e75b0",
"season_number": 20,
"name": "Season 20",
"number_of_episodes": 9
}, *and so on...*
]