0

I need an advice! I have an array of objects with many students data(more then 200 students). So, now, i want to implement lesson for this students, so, every day, i will push an array with data inside every array of students. Later i will work with all lesson data! So my question is: a) Is the best way to push array inside of every students array? b) Or make another array with unique _id, and later filter lesson by students _id?

So, i'm looking for performance and speed...

1 Answers1

0

As I understood from your architecture, a suitable option will be moving lessons to a separate collection and storing lesson._id in students[].lessons. You can reach it by using ref property in your mongoose schema.

Here's the example:

lessons collection data:

[
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "name": "First lesson"
  },
  {
    "_id": ObjectId("5a934e000102030405000002"),
    "name": "Second lesson"
  }
]

groups collection data:

[
  {
    "_id": ObjectId("5a934e000102030405000003"),
    "name": "Group 1",
    "students": [
      {
        "_id": ObjectId("5a934e000102030405000004"),
        "name": "John",
        "lessons": [ObjectId("5a934e000102030405000001")]
      },
      {
        "_id": ObjectId("5a934e000102030405000005"),
        "name": "James",
        "lessons": [ObjectId("5a934e000102030405000001"), ObjectId("5a934e000102030405000002")]
      }
    ]
  }
]

But I would also moved every student to separate students collection if it is possible (if you currently have students as array field).

Hlib Derbenov
  • 916
  • 1
  • 7
  • 15
  • Thx for your answer, but this is the question, should i use separate collection, or only one? – Ruslan Jackson Feb 15 '20 at 19:41
  • 1
    @RuslanJackson please have a look at this answer https://stackoverflow.com/a/15844726/9317057 (nested array vs separate collection). If we talk about short answer, then mine is: use nested array if you 100% sure that lessons will be strictly bound to students and won't be used without them. **But** if we talk about scalability and possibility to update lesson data by changing one document only (and using refs), then I recommend using separater collection. So that's not perfomance issue only. – Hlib Derbenov Feb 15 '20 at 20:13