On creating separate collections for indexing, schema design and more

Hi there,

I have a specific use case that I’m wondering about, and hope someone has some insight on this :slight_smile:
I am making an application that revolves around the schema character which looks like this (typescript):

export interface ICharacter {
  _id: string | mongoose.Types.ObjectId;
  name: string;
  role: string;

And the idea is that you have a search bar where you can start typing to search on all the options for role, which would bring up an auto-complete-list of suggestions for roles. These suggestions/options are based of off the values previously assigned to any character document.

E.g. if we have the following characters:

  name: 'Mark',
  role: 'Gardener'
  name: 'Eric',
  role: 'Graphic Designer'
  name: 'Patric',
  role: 'Web Developer'

Then typing “G” would suggest Gardener and Graphic Designer. I’ve tried to apply the autocomplete index to make this work but only managed to get a list of the documents that have this role assigned to them, which is not what I’m looking for. I’m rather imagining I need to create an index based on the “distinct” role values / options drawn from the values characters currently have assigned to them, which would be used for auto-completion. E.g. having an index with “Gardener, Graphic Designer, Web Developer”. Then the index to run autocompletion on would be updated as soon as a character gets a role that’s new to the dataset.

The only way I see how to do this is to create a separate collection for roles and create an index from that collection. It would work but it would be super neat to have the role property embedded in the character schema rather than as a relationship.

Would be great to know what’s best practice in this circumstance :slight_smile: