Good practice for parent model field udpate with mongoose

Maxime Flament

I have 3 models having this relationship:

Course ==> [Chapter] ==> Quiz

i.e., I have a Course model containing an array of ObjectId referencing Chapter documents, and my Chapter model contains an ObjectId referencing a Quiz document.

In my Course schema definition, I have a lastUpdated field which stores the date at which the last modification of the course was performed.

Schemas definition are given below:

Course schema:

let courseScheme = mongoose.Schema(
    {
        title: { type: String, required: true },
        nbChapters: { type: Number, default: 0 },
        chapters: { type: [mongoose.Schema.Types.ObjectId], default: [], ref: 'Chapter' },
        lastUpdated: { type: Date, default: Date.now() }, // I want this field to be updated if I create/update/delete a reference or a transitive reference of this document
    }
);

Chapter schema:

let chapterScheme = mongoose.Schema(
    {
        chapterName: {type: String, required: true},
        chapterIndex: {type: Number, required: true}, // used to sort chapters on client side (for ex: in navigation menu)
        course: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'Course' },
        quizzId: {type: mongoose.Schema.Types.ObjectId, ref: 'Quiz'},
        // some more fields
    }
);

Quiz schema:

let quizScheme = mongoose.Schema(
    {
        questions: [
            {
                questionIndex: {type: Number, required: true}, // used to sort questions on client side
                type: {type: String, required: true, enum: ['MultiSelect', 'Select', 'Input']},
                question: {type: String, required: true}
                // some more fields
            },
        ]
    }
);

I'd like to know if there's any good practice to update the lastUpdated field in my course document, if I make a modification to any of its child (direct/indirect). For example, if I add a new quiz reference to a chapter of a given course, how would one update the lastUpdated field of the corresponding course?

I've thought of doing the following:

  • Using hooks to update this field everytime I update/save a new child document of a course
  • Directly query the parent course and update its field whenever required

PS: I'm not asking for code as an answer, just some hints about what are common good practices for this kind of operations.

Z-Bone

I'd say you have 2 possible approaches:

  1. Handle when updating the DB
    Update the lastUpdated field upon changes to linked schemas. You have a few approaches for this (some you have mentioned)

    • Use 2 operations (2 requests/2 operations): update chapters than update courses
    • Use a hook (also 2 requests/2 operations)
    • Use a batch/bulk update (1 request, 2 operations): this would be most efficient since you only send one outgoing request to MongoDB. However, still 2 operations occur on the MongoDB server.
  2. Handle when querying the DB
    Don't update the lastUpdated field on the courses collection if a change was made to another collection. Instead, keep a lastUpdated field on all relevant collections, and choose the most recent date when querying.

    Example
    When you are querying your data, use a $lookup aggregation on two collections, chapters and courses. On the result, chose the most recent lastUpdated date - either from courses or from chapters.


Hope this helps

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Is casting "this" from a parent class method to a child a good practice?

分類Dev

Why Mongoose saves references of child ids in parent model?

分類Dev

Is import static a good practice?

分類Dev

is #define PFUser good practice

分類Dev

Is it a good practice to return empty body?

分類Dev

Mongoose sort by populated field

分類Dev

Mongoose unique field handling

分類Dev

Mongoose find users with a field?

分類Dev

Retrieving model using mongoose

分類Dev

Is it good practice to have all classes to have inheritence

分類Dev

Is it a good practice to use Java Streams in POJO as attributes

分類Dev

Good practice design pattern for Exception handling

分類Dev

Is it a good practice to log error inside exception constructor?

分類Dev

Is it a good practice to have config in Redux store?

分類Dev

Is creating an accounts app in Django a good practice?

分類Dev

Is it good practice to commit multiple files at once?

分類Dev

Flexibility vs performance considered good practice

分類Dev

Is it a good practice to close dynamic shovel in RMQ?

分類Dev

Good practice to parse data in a custom format

分類Dev

is it good practice to encode user input to database?

分類Dev

Is it good practice to put static methods in Laravel models?

分類Dev

Is that a good practice to use WebView instead of Textview?

分類Dev

How to use Symbols with Mongoose model?

分類Dev

customising a model field for serialization

分類Dev

Model method with ManyToMany field

分類Dev

Which statement is good for clear the text field

分類Dev

Is it a good practice to define C++ functions inside header files?

分類Dev

Is it a good practice use Django integrated web server behind a proxy?

分類Dev

Is it good practice to copy objects from the stack to the heap by setting a dereferenced pointer?

Related 関連記事

  1. 1

    Is casting "this" from a parent class method to a child a good practice?

  2. 2

    Why Mongoose saves references of child ids in parent model?

  3. 3

    Is import static a good practice?

  4. 4

    is #define PFUser good practice

  5. 5

    Is it a good practice to return empty body?

  6. 6

    Mongoose sort by populated field

  7. 7

    Mongoose unique field handling

  8. 8

    Mongoose find users with a field?

  9. 9

    Retrieving model using mongoose

  10. 10

    Is it good practice to have all classes to have inheritence

  11. 11

    Is it a good practice to use Java Streams in POJO as attributes

  12. 12

    Good practice design pattern for Exception handling

  13. 13

    Is it a good practice to log error inside exception constructor?

  14. 14

    Is it a good practice to have config in Redux store?

  15. 15

    Is creating an accounts app in Django a good practice?

  16. 16

    Is it good practice to commit multiple files at once?

  17. 17

    Flexibility vs performance considered good practice

  18. 18

    Is it a good practice to close dynamic shovel in RMQ?

  19. 19

    Good practice to parse data in a custom format

  20. 20

    is it good practice to encode user input to database?

  21. 21

    Is it good practice to put static methods in Laravel models?

  22. 22

    Is that a good practice to use WebView instead of Textview?

  23. 23

    How to use Symbols with Mongoose model?

  24. 24

    customising a model field for serialization

  25. 25

    Model method with ManyToMany field

  26. 26

    Which statement is good for clear the text field

  27. 27

    Is it a good practice to define C++ functions inside header files?

  28. 28

    Is it a good practice use Django integrated web server behind a proxy?

  29. 29

    Is it good practice to copy objects from the stack to the heap by setting a dereferenced pointer?

ホットタグ

アーカイブ