Create different types of many-to-many relationships within the same collection

I have a ‘Producer’ collection, which are companies that offer services or goods. And I want to create relationships (of different types) between these companies.

For example: a company produces barley malt. This company has a relationship with another company (a farmer, for example) from which it buys barley. And it also has another relationship with another company (a brewer, for example) to which it sells barley malt.

These relationships must have a type. Each type of relationship has its reciprocal or opposite type of relationship. Some are: ‘provider’/‘providerOf’, ‘member’/‘memberOf’. So if one company has a ‘provider’ relationship with another, this second company must have a ‘providerOf’ relationship with the first company.

I want to be able to easily perform the following actions:

  • Add a relation to a producer
  • When adding a relation to a producer, add the inverse relation to the related producer, automatically. For example: if I add the relationship of the barley malt producer with the farmer who is its ‘provider’, I also need the farmer to have the ‘providerOf’ relationship with this malt producer.
  • The same is necessary when deleting a relationship.
  • When reading a producer, get the data of all its relations, with the type of these relations.

So, could you please share the best practices of handling such kind of relationships in MongoDB?

I have started to do it in the following way, but I am not sure it is a good way. I put links below to the code, as it is open.

I have created a property on the ‘Producer’ collection:

    relations: {
        type: Map,
        of: [{
            type: mongoose.Types.ObjectId,
            ref: 'Producer'
        enum: relationTypeEnum
const relationTypeEnum = [

So, I am creating an endpoint to add a relationship and an endpoint to remove it.

And in the controller I am creating methods to add a relation in a document and the inverse relation in the document that the first document is related to.

Is this a good relationship model or can it be done better?