Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
9 replies
JD

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"


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"
        }
    }
}


Would really appreciate any suggestions.
GitHub
: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
SpaceX-API/queries.md at master · r-spacex/SpaceX-API
Was this page helpful?