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 Answer 1

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

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

2 Comments

Thx for your answer, but this is the question, should i use separate collection, or only one?
@RuslanJackson please have a look at this answer 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.

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.