D
D
decvdence2021-05-30 19:10:02
JavaScript
decvdence, 2021-05-30 19:10:02

How to organize MongoDB database structure?

I'm creating a simple note-taking app like google keep.
Each user has notes and a list of labels, each note can have multiple labels.
Actually the question is: should I create separate models for labels and notes, or store them in the user's document? If separately, how best to group them for each user?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew Hecc, 2021-05-30
@decvdence

It really all depends on the functionality that you want to implement.
I would do it all as separate models, so it will be possible to implement their normal editing and modification, with a pull-up to all created entities.
That is, the structure would look something like this:

User {
    _id: ObjectId('...'),
    notes: [
        ObjectId('Note1...'),
        ObjectId('Note2...'),
        ObjectId('Note3...')
    ]
}


Note {
     _id: ObjectId('Note1...'),
     ...,
    labels: [
        ObjectId('Label 1'), 
        ObjectId('Label 2'),
    ]
}


Label {
    _id: ObjectId('Label 1'),
    name: "Label 1"
}

Again, formally, labels can be stored directly in the records, but then such a task as filtering by some of the labels becomes unreasonably more difficult + it will be more difficult to maintain data consistency.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question