Many to many relation table field into MongoDB

While doing a many to many relationship in SQL we create a table that will have the ID of the 2 tables that are forming the relationship. Sometimes in that table that we are creating, we put more data.


A table called worker and a table called department. Workers can work in many departments and many workers can work in the same department.

However the workers move between department and we need to store the year that they move into another department.

In mongoDB how can we represent that year. I am creating id for every worker and adding the department where they have worked but how do I store the year?

How do you do it in SQL? What ever you do in SQL could be done here.

So they always switch department on an exact year boundary. Nobody ever moves from one department to the other on May 4th for example?

I think it would be better to have a date rather than a year.

Like I wrote, you could do the same:

collection Workers

  "_id" : worker_primary_key_value ,

collection Departments

  "_id" : department_primary_key_value ,

collection Entity

  "worker_id" : worker_primary_key_value ,
  "department_id" : department_primary_key_value ,
  "start_date" : ISODate( "2022-04-29" )

But you would not leverage the flexible schema nature of MongoDB, see