How to filter the SpaceX API (mongoDB find() based) by a nested array?! Tech Test

I'm stuck on a tech test where I need to call the spaceX API and filter all launches in the response based on an array of payloads including a specific nationality string. The the response normally has only a list of payload ids, but the full payload data is populated via options object. Link to API docs: https://github.com/r-spacex/SpaceX-API/blob/master/docs/queries.md I've been going through all the mongoDB docs trying to get my head around the syntax, but keep either getting all data unfiltered, or none, or an error. Been trying for days, and the I know from the recruiter that others have failed because they failed to add in this specific filter in past tech tests. I'm currently getting this error:
Cast to ObjectId failed for value "{ nationalities: 'United States' }" (type Object) at path "payloads" for model "Launch"
Cast to ObjectId failed for value "{ nationalities: 'United States' }" (type Object) at path "payloads" for model "Launch"
This is the body I am sending:
{
"query": {
"payloads": {"nationalities": "United States"}
},
"options": {
"pagination": true,
"select": {
"date_utc": 1,
"links": 1,
"payloads": 1,
"flight_number": 1,
"name": 1,
"success": 1,
"upcoming": 1
},
"populate": [
{
"path": "payloads",
"select": {
"nationalities": 1
},
"retainNullValues": true
}
],
"sort": {
"date_utc": "desc"
}
}
}
{
"query": {
"payloads": {"nationalities": "United States"}
},
"options": {
"pagination": true,
"select": {
"date_utc": 1,
"links": 1,
"payloads": 1,
"flight_number": 1,
"name": 1,
"success": 1,
"upcoming": 1
},
"populate": [
{
"path": "payloads",
"select": {
"nationalities": 1
},
"retainNullValues": true
}
],
"sort": {
"date_utc": "desc"
}
}
}
Would really appreciate any suggestions.
GitHub
SpaceX-API/queries.md at master · r-spacex/SpaceX-API
:rocket: Open Source REST API for SpaceX launch, rocket, core, capsule, starlink, launchpad, and landing pad data. - SpaceX-API/queries.md at master · r-spacex/SpaceX-API
6 Replies
JD
JDOP2y ago
To clarify, I'm trying to avoid filtering locally so I can use the APIs built in pagination. Worst case I assume the best option is to query the payloads end-point instead to get a list of all launch ids and then query the Launch API with those. 🤷‍♂️
Vengeance
Vengeance2y ago
Try
"query": {
"payloads.nationalities": {
"$in": ["United States"]
}
}
"query": {
"payloads.nationalities": {
"$in": ["United States"]
}
}
JD
JDOP2y ago
nope. 200 status, but docs array is empty
Vengeance
Vengeance2y ago
I tried $in, $all, and $elemMatch and even $exists and none of them work for me lmao
"query": {
"payloads": {
"$elemMatch": {
"nationalities": {
"$exists": true
}
}
}
}
"query": {
"payloads": {
"$elemMatch": {
"nationalities": {
"$exists": true
}
}
}
}
returns nothing for me O_o
JD
JDOP2y ago
Thanks for trying. At least I can be satisfied it's not just me being stupid. I think it's due to the field being populated in the options. It can only be filtered on the payload id string 😭 The advice on stack overflow was "just change the API scheme" 😂
Vengeance
Vengeance2y ago
lmao maybe ask gpt3 kek
Want results from more Discord servers?
Add your server