'Limit array of references to documents that have not been referenced yet

I have a "reference selector" (i.e. a module where editors can choose documents to reference) but I want to limit the results to the documents that have not been referenced yet.

This is the schema of my reference selector that is used inside another document:

export default {
  title: 'Project Selector',
  name: 'projectSelector',
  type: 'object',
  fields: [
    {
      title: 'Project',
      name: 'project',
      type: 'reference',
      to: [
        { type: 'project' }
      ],
      options: {
        filter: // What does my filter need to look like?
      }
    }
  ]
}

export default {
  title: 'Home Page',
  name: 'homePage',
  type: 'document',
  fields: [
    {
       title: 'Projects',
       name: 'projectsGrid',
       type: 'array',
       of: [
         { type: 'projectSelector' }
       ],
    },
  ]
}

Please note, the projectSelector object and the homePage document do not share any parent<>child relationship.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source