0

Currently, I have two relevant Documents, Embeddings and Employees:

  • Employees can have a lot of Embeddings related to them
  • Embeddings can have multiple Employees related to them.

My use-case is that I need to query Employees by Embedding IDs. So I receive a list of Embedding IDs and I need to get all the related employees from the DB.

My first thought was to store a list of Employee ID's in the Embedding documents. The issue I'm having there is that Employees of course can leave a company so in that case, I would get not found errors...

The other option I see is to reference Employees in the Embedding documents but in the case where an employee is related to a large set of Embeddings, it would also not be ideal...

Is there a best practice in a many-to-many scenario like this?

rvwsd
  • 97
  • 11
  • Does this answer your question? [MongoDB Many-to-Many Association](https://stackoverflow.com/questions/2336700/mongodb-many-to-many-association) – sergmister Jun 16 '21 at 22:35
  • Thanks for your reaction. But no, that post actually repeats the two options I suggested in my question. I was wondering if there is some design for this kind of use case but might be that it just does not exist. – rvwsd Jun 16 '21 at 22:39

1 Answers1

1

I do not know what exactly your overall use case is but there are several patterns (best practises) to follow depending your use case. You can find more info here: https://www.mongodb.com/blog/post/building-with-patterns-a-summary

Iirc there are some similar cases covered.

Personally I would most likely do something like:

Employee{
_id:ID
embeddings: Array[ID]
}

Embedding{
_id:ID
}

This would make it easier to update when someone leaves a company and good enough if you want to find all the employees of a specific embedding.

kakou
  • 636
  • 2
  • 7
  • 15