1

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) :

enter image description here

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

1
  • 2
    C# is frequently used with mongodb Commented Dec 9, 2018 at 2:10

2 Answers 2

3

If I understand you problem correctly you have to migrate existing C# + SQL program to GO + Mongo stack. You are trying to address it asking technical questions like is the current structure good.

Something is missing here.

Every technology is just a solution for business problem. What is business problem here? Your question does not explain that.

Try to get answers to questions below before we can move to technical side:

  1. What is wrong with C# and SQL solution, so we have to replace it with something new?
  2. What does not work in existing RDBMS data model?

In any case be careful trying to apply your RDBMS and ORM patterns to mongo DB. It is very different type of data storage (document DB) and RDBMS model cannot be just copied into it.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey ! Thanks for your answer, it's about migrating you know, and I just wanted to have thoughts about what's already done. We will switch for sure (for internal reasons) and since mongodb is something new for us (since we did a lot of SQL), we are just questioning ourself if we should store nested objects or do different documents. With different documents, the evolution seems easier to us, but since we can store nested data, then we're pretty sure it would make it way faster you know.. That's why I wanted to have externel thoughts from dev/expert here on stackoverflow
With document database typically data stored in denormalized (to some reasonable degree) form. One document entry per business entity.
2

1- The current structure seems ok. Of course, it will depend on your particular case, but it seems to be good.

2- You can, but you don't have to. It makes sense to only have one document when your data is not relational. In your case, it probably makes more sense to keep them separated, otherwise you may have a lot of repeated entries. You can use the $lookup aggregation to perform joins between documents.

Just be careful with $lookup. The last time I read about it, Mongo did not support this operation if your data was sharded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.