Is there a way to query for [exists] = false or [in] ? Other suggestions welcome :)

Edit: I found a workout, see my comment below.

Hey there Contentful Community!

I’m in the process of setting up a new Angular app, using Contentful for the first time, and I’m liking it a lot so far :slight_smile:

Is it possible to set query params to check if either, a field doesn’t exist or if it does exist and has a specific id?

I’ve managed to do each part of this separately. This for example, returns jobs where no countries have been selected:

'[exists]': false

And, the following returns jobs which have been filed under a specific country id.

'[in]': 'Lw9e5ljC7M2UmgGi2ftZk'

But when joined together, I get nothing :slight_smile:

  content_type: 'job',
  '[exists]': false,
  '[in]': 'Lw9e5ljC7M2UmgGi2ftZk',

Background and longer explanation

I have a content type, Jobs, which has a one-to-many reference field called Countries that accepts entries from another content type… Countries (this is one example, but applies to many fields).

On the frontend you can select a country to show jobs from. I can specify the selected country ids in the query and it works well.

'[in]': 'Lw9e5ljC7M2UmgGi2ftZk'

However, I would also like jobs with no countries selected, to act like jobs with all countries selected. I can achieve this by checking to see if the field exists:

'[exists]': false

However, when I combine both, it fails, presumably as it’s treat the request like exists = false and is in id x.

The main reason why I would like countries (or any other reference field) to behave this way, is that it would be too much effort to link every country manually, potentially nearly 200.

Alternatively a select all checkbox in the Contentful editor would work, but that doesn’t seem to exist!

If anyone has any ideas of how I could achieve this, I’d be super grateful. Or if anyone has any workaround, I’d love to hear them.

Thanks in advance and please let me know if I’ve not explained anything clearly.


I’ve found a workaround for this by querying [nin] and the difference of items between what I want and don’t want. For example:

allItems = [1,2,3,4,5];
wantedItems = [1,2];
unwantedItems = allItems.filter(el => !wantedItems.includes(el)); // [3,4,5]
// Query options
fields.<name>[nin]': unwantedItems.join(',')

As per my example above of jobs + countries, this query works as it returns jobs that have no countries linked, as they don’t have the ids specified.