2

I'm trying to filter all products without categories (which is array of references) and can't find how to do it.

S.listItem()
  .title('Without category')
  .id('productsWithoutCategories')
    .child(
      S.documentList()
        .title('Without categories')
        .menuItems(S.documentTypeList('product').getMenuItems())
        .filter('_type == $type && !category')
        .params({ type: 'product' })
      )

!category not working. I will be grateful for the help.

2 Answers2

3

Finded answer with Vision (really pleasant tool)

https://www.sanity.io/docs/the-vision-plugin

 .filter('_type == $type && !defined(categories)')
 .params({ type: 'product' })
2

As you've already found out yourself, you can use the defined function to check if something is set (or not set by using the NOT operator !). You can find some documentation regarding the defined function here:

Just want to quote from the documentation as well:

You'll never see a null value on a document stored in Sanity. If you update a value to null, that key will disappear. Thus, the is null constraint you may know from SQL does not bear any meaning here. A value is either defined or !defined.

And from the How Queries Work page:

Filtering by the presence of a field, e.g. *[defined(status)] which only match document that have the status property set to any value.

Anders Stensaas
  • 749
  • 5
  • 18