我应该将活动时间表放置在mongodb中,嵌入到用户中还是单独放置在哪里? [英] Where should I put activities timeline in mongodb, embedded in user or separately?

查看:71
本文介绍了我应该将活动时间表放置在mongodb中,嵌入到用户中还是单独放置在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个电子学习应用程序,并将学生活动显示为时间轴,应该将它们嵌入到 user 集合中,还是使用 userId



约束:




  • 一对多关系。

  • 用户活动详尽而众多

  • 在90%的时间里,我们一次只需要看到一个用户,而另一例是
    是主管(老师)需要查看
    用户活动的摘要的地方(也许是另一个收藏集?)

  • 我没有想到用例寻找活动并寻找学生的方法,也许以后我会用到它吗? (例如,先看看谁完成了某项特定的活动?但这将关系变为多对多,这是一个完全不同的问题)



我在这两个问题中发现了相关问题的不同模式:




解决方案

您不应将活动嵌入学生文档中。 / p>

Th我对此很有信心的原因是以下语句:



用户活动详细且数量众多

将学生活动显示为时间轴

老师需要查看用户活动的摘要



设计不断增长的架构是一种不好的做法文档-因此,拥有一份学生文档,每次他们完成/添加另一项活动时都会保持增长,这是导致表现不佳的秘诀。



如果您想对学生的活动进行排序,如果每个都是活动集中的单独文档,则比每个学生文档中的数组要简单得多。



当您需要查询多个学生的活动时,所有单个集合中的活动会使它变得微不足道,但是将活动嵌入到学生文档中会很困难(很可能需要聚合框架,这会使它变慢)。



您还说您可能有d将来看谁先完成了某些特定活动?但这将关系更改为多对多,这是一个完全不同的问题-情况并非如此。您无需将其视为多对多关系-您仍可以存储与单个关联的多个活动用户,然后查询与活动 X匹配的所有记录,并按完成时间(或其他时间)排序,并查看哪个学生的时间最短。


I am building an e-learning app, and showing student activities as a timeline, should I embed them in the user collection, or create a separate collection with an userId.

Constraints:

  • One to many relationship.
  • User activities are detailed and numerous
  • For 90% of the time, we only need to see one user at an time, the other case is where a supervisor(teacher) needs to see an summary of the activities of users(maybe another collection?)
  • I haven't thought of the use case of searching for activities and finding students, maybe I'll have a use for this later on? (eg. see who finished some particular activity first? But that changes the relationship to be Many to many and is a completely different question)

I have found different schemas for the related problem in these two questions:

解决方案

You should not embed activities into student document.

The reason I'm pretty confident of this is the following statements:

"User activities are detailed and numerous"
"showing student activities as a timeline"
"teacher needs to see an summary of the activities of users"

It is a bad practice to design schema that has ever-growing documents - so having a student document that keeps growing every time they complete/add another activity is a recipe for poor performance.

If you want to sort student's activities, it's a lot simpler if each is a separate document in an activity collection than if it's an array within a student document.

When you need to query about activities across multiple students, having all activities in a single collection makes it trivial, but having activities embedded in student documents makes it difficult (you will need aggregation framework, most likely, which will make it slower).

You also say you might have need in the future to "see who finished some particular activity first? But that changes the relationship to be Many to many and is a completely different question" - this is not the case. You do not need to treat this as a many-to-many relationship - you can still store multiple activities associated with a single user and then query for all records matching activity "X" sorting by time finished (or whatever) and seeing which student has lowest time.

这篇关于我应该将活动时间表放置在mongodb中,嵌入到用户中还是单独放置在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆