dongtan9253 2018-12-09 00:42
浏览 139

从SQL(C#和实体框架)到mongodb(Go和mgo)

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

  • 写回答

2条回答 默认 最新

  • douxian7117 2018-12-09 03:11
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突