Correct way of querying Content API with $in

Hi there, this may be a bit of a basic question but I’m having trouble making a query with the $in command in a query.

I’ve attempted this:

https://builder.io/api/v2/content/profile?apiKey=ab24932172a5427f981ba9d6a9b615a6&query.data.sku.$in=20202020","10101010
But receive an error claiming I haven’t submitted an API. So when I used JS-style arrays it doesn’t return my results ie:
https://builder.io/api/v2/content/profile?apiKey=ab24932172a5427f981ba9d6a9b615a6&query.data.sku.$in=[20202020,10101010]
I also attempted with double quotes between the values.

I just want to make sure that I’m querying properly and my syntax isn’t incorrect. If this should work then perhaps it’s something else causing my trouble.

Also it would be nice if this was clarified because $in as a Mongo query in JSON works differently than the query string variant it seems. I could be wrong.

Thanks,

David

Hey @davedbase ! The issue here is that the API is seeing your values (20202020, 10101010) as numbers when they are set in your model schema as a string. To force it to see it as a string you need to use either double ", or single quotes ' .

It looks like you attempted this in your first call a bit, but notice some of the " seem to be removed, you also need to escape the quotes to force them to be read as string.

Also, your query above is to the profile model, but I think you mean to use the product model. So this, should work:

https://builder.io/api/v2/content/product?apiKey=ab24932172a5427f981ba9d6a9b615a6&query.data.sku.$in=[“\“20202020\””,“\“10101010\””]

where the query is query.data.sku.$in=["\"20202020\"","\"10101010\""]

Try that out and see if it works!

Sigh :man_facepalming: :man_facepalming: :man_facepalming:

Well that’s slightly embarrassing. Thanks for clarifying it for me I appreciate your patience. :slight_smile:

1 Like