API query with enums

I have this type of data model:

And I would like to query for: industry, solution, and country.
These are all enums:

Now from my code, I would like to query for these in a 3 and condition:

This is the data-model api explorer link: https://cdn.builder.io/api/v2/content/barion-userstory-data?apiKey=32f148657e2646be8562eb4e6ebfa190&limit=1

This is how I try fo fetch from my code:

 const queryArray = []

    if(solution){
        queryArray.push({
            solution: {
                $elemMatch: solution.key
            }
        } )
    }
    if(industry){
        queryArray.push({
            industry: {
                $eq: industry.key
            }
        } )
    }
    if(country){
        queryArray.push({
            country: {
                $eq: country.key
            }
        } )
    }
    

    console.log(queryArray)

    const result=await builder.getAll('barion-userstory-data', {
        options: {
          noTargeting: true,
          query: {
              $and: queryArray
            },
        },
        limit: 9999999,
        omit: 'data.blocks',
      })
      console.log(result)

How my queryArray looks like:

What I try to achieve is, retrieve the data which are:

  • The data’s country property equals the country type
  • The data’s industry property equals the industry type
  • The data’s solution list property contains the solution type

Example query:

(https://cdn.builder.io/api/v1/query/32f148657e2646be8562eb4e6ebfa190/barion-userstory-data:62235058?omit=data.blocks&apiKey=32f148657e2646be8562eb4e6ebfa190&noTargeting=true&query.$and.0.solution.$elemMatch.type=sol1&query.$and.1.industry.$eq.type=type3&query.$and.2.country.$eq.type=country2")

Hope you can help me, because it always returns empty list which is not good enough.

Hi @radikris, is this the result you were looking for?
https://cdn.builder.io/api/v2/content/barion-userstory-data?apiKey=32f148657e2646be8562eb4e6ebfa190&query.data.country.type.$eq=country3&query.data.industry.type.$eq=type3&query.data.solution.$elemMatch.solution.type=sol

Here is the query I used in the API explorer:

{
 "query.data.country.type.$eq": "country3",
  "query.data.industry.type.$eq": "type3",
  "query.data.solution.$elemMatch.solution.type": "sol"
}
1 Like

@sarah thank you very much, yes pretty much that’s what I want to achieve.
My only problem is, that from code it is not working, can you please check this out?

      const result=await builder.getAll('barion-userstory-data', {
        options: {
          noTargeting: true,
          query: {
                 $and: [{
                    "data.industry.type.$eq": 'type3',
                    "data.country.type.$eq": 'country3',
                    "data.solution.$elemMatch.solution.type": "sol"
                  }]
              },
              },
        
        limit: 9999999,
        omit: 'data.blocks',
      })


This is working:

    const result=await builder.getAll('barion-userstory-data', {
        options: {
          noTargeting: true,
          query: {
            "data.country.type.$eq": "country3",
             "data.industry.type.$eq": "type3",
             "data.solution.$elemMatch.solution.type": "sol"
          },
        },
        
        limit: 9999999,
        omit: 'data.blocks',
      })

Hi @radikris,

The working example you shared should achieve the same result. Is there any reason why that wouldn’t work for you?

it is working, but I am curious why the $and syntax won’t work, because I guess it should be