I’m trying to model the data as follows. Is this good practice or is there a better way so that I don’t have to repeat the data in the checklist collection.
/* technical book 1
reference number
subject area
topic
checklist1
note1
note2
note3
note4
//many more notes
checklist2
note1
note2
note3
//many more notes
checklist3
note1
note2
note3
//many more notes
*/
// the note document structure - there could be many thousands of notes
[
{
_id: 8172931,
noteTitle: "my 1st note title",
noteText: "something interesting",
noteRef: "other information",
belongsTo: 27, //this is the checklistId
},
{
_id: 8172932,
noteTitle: "my 2nd note title",
noteText: "something interesting",
noteRef: "other information",
belongsTo: 28, //this is the checklistId
},
];
// the checklist document structure - there will be about 1000 checklists
[
{
_id: 27,
checklistName: "1st checklist name",
checklistTopic: "a topic",
checklistSubject: "a subject",
checklistRefNum: "f-38",
checklistBookName: "the book that the checklist belongs to",
},
{
_id: 28,
checklistName: "2nd checklist name", //this will be unique
checklistTopic: "a topic", //this will be repeated
checklistSubject: "a subject", //this will be repeated
checklistRefNum: "g-32", //this will be repeated
checklistBookName: "the book that the checklist belongs to", //this will be repeated
},
];
The UI for the app will display a list of notes when the user has selected a single checklist. The checklists will be displayed based on the selection of
BookName
RefNum
Subject
Topic
Thus the query for the checklists will be… (UI will update on user selection)
const agg = [
{
'$match': {
'checklistBookName': 'example name',
'checklistRefNum': 'f-38',
'checklistSubject': 'a subject',
'checklistTopic': 'a topic'
}
}
];
const coll = client.db('databaseName').collection('checklists');
const cursor = coll.aggregate(agg);
const result = await cursor.toArray();
The corresponding notes will be retrieved then based on the checklist that they belong too. i.e. find all notes with the belongsTo = the checklist ID.
Is this good practice or is there a better way?