简体   繁体   中英

Doubts about design Shift schema in MongoDB

I am coding an API Rest for a small project about Shifts for the employees but I am stuck in how to design the models. I found a way to design it but I have doubts if it is the best way.

My collections are:

  • Users: (Employees)
  • Positions: (Employee Position)
  • Shift: ( start - end time based on the employee position.)
  • Record: (day of the shift, shift and employees)
  • Rota: (information about rota)

Users can have more than one position.

The shifts only have one position.

Records collect the shifting the array of employees and references about rota.

I will simplify my code:

const User = Schema({
   name: {type: String},
   position: [{type: Schema.Types.ObjectID, ref 'Positions'}]

});
const positions = Schema({
   name: {type: String}
});
const Shifts = Schema({
   start_time: {type: Date},
   end_time : {type: Date},
   position: {type: Schema.Type.ObjectID, ref 'Positions'}
});
const Records = Schema({
   start_date: {type: Date},
   end_date : {type: Date},
   shift: {type: Schema.Type.ObjectID, ref 'Shifts'},
   employees:[{type: Schema.Type.ObjectID, ref 'Users'}]

});

Do you think it is a good design to develop the schema or not?

Anything to improve? or something to change?

From my experience too many references in MongoDB slows the MongoDB. Your design is good when you are relational database.The concept of a Document database is that it eliminates lots of joins. Try to place as much in a single document as you can. Because MongoDB documents have structure, and because you can efficiently query within that structure Also you don't have to normalise the data like you would in SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM