I am coming today because I have to migrate from SQL (with entity framework) to MongoDb, however, the database side of programming is a field where I am not an expert and I would like to make the best choice for the evolution of the program I am working on.
Let say I have this database schema (an idea, not the actual case) :
So I have a school. In this school, I have classes and, those classes have students. The foreign keys are the italic fields.
It should give something like that in C#:
public class SchoolEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; } = new Guid();
public string Name { get; set; }
[InverseProperty("School")]
public virtual ICollection<ClassTeachingEntity> ClassTeachings { get; set; }
}
public class ClassTeachingEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; } = new Guid();
public Guid SchoolId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[ForeignKey("SchoolId")]
public virtual SchoolEntity School { get; set; }
[InverseProperty("ClassTeaching")]
public virtual ICollection<StudyingEntity> StudyingEntities { get; set; }
}
public class StudyingEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; } = new Guid();
[Index("UniqueStudyingRelation", Order = 1, IsUnique = true)]
public Guid UserId { get; set; }
[Index("UniqueStudyingRelation", Order = 2, IsUnique = true)]
public Guid ClassTeachingId { get; set; }
[ForeignKey("UserId")]
public virtual UserEntity User { get; set; }
[ForeignKey("ClassTeachingId")]
public virtual ClassTeachingEntity ClassTeaching { get; set; }
}
public class UserEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; } = new Guid();
public string Firstname { get; set; }
public string Lastname { get; set; }
[InverseProperty("User")]
public virtual ICollection<StudyingEntity> StudyingEntities { get; set; }
}
My question is, first, is the current structure good? (I didn't designed it, it has been realized before I arrived and I am more a beginner than an expert in databases)
The second question is, do I have to create just one document from my four different tables? Because, as far as I know, in mongodb, it's a JSON logic, so I can store nested objects right?
I don't know if this information matters but I will use Go with mongodb, not C# anymore.
Thanks for your feedback