Sunday 20 October 2019

Need help with MongoDB database structure

Hey,​I am making a server synced diary application with NodeJS, and using MongoDB. I have all my super relational data in MySQL. But for users Daily memoirs im going to use Mongo, because as you may have realised, there will be a crap load of notes/day diaries, and I want to learn MongoDB, and it is supposed to be way better for lots of non relational data.​I have learned how to create DBs and do everything, but something all the tutorials dont cover, is the most important thing of all, how do I structure my data?​Down below I have several examples of what ive thought, and as I am pretty unexperienced with Mongo, I would like some advice, on which option would be the best performance wise.​Thank you in advance for your time, and any help!​Example 1: My database has one HUGE collection called “Days” and each entry to that collection looks like this: (I am sorry, but no matter how much I think about it, this sounds like the least performant option, as said I am unexperienced in Mongo, and might be wrong.)​{userID: 902, //This user ID will be fetched from MySQL when authenticating users request. From what ive read, I need to run a command similar to this: “db.posts.createIndex( { author_name : 1 } )”, on this collection to somehow optimize performance? //What day? No, I wont use Date for this, because then id have to turn my JSON Query data to Date before querying (Maybe I wouldnt have to, as Mongo may store it as string anyway). BUT, I am not sure whether i should use 3 separate Integer fields, or one string field. Which would be faster? (EDIT: I know three separate fields with int will be WAY faster, as my application also has to query data for one month, etc. MAYBE Im wrong, and this is bad practice, let me know.) day: 12, month: 5, year: 2018, //Actual stored data: dayTitle: “Lame day at home..”, dayDescription: “Installed arch..”, hugeLoadOfIndividualSmallNotesForThisDayWithTimeStamps: [ { data: “Woke up, start now”, time: “9:44”, { data: “Finally figured out what fdisk is”, time: “21:29” } } … ] }​Example 2: My database has a collection for each user which is named by their userID (This sounds VERY good and organized to me, and with my common sense, it would sound like the most performance one, but from what I googled, people said this wouldnt be good, and thats EXACTLY why I am asking here), and each entry to that collection looks like this:​{day: 12, month: 5, year: 2018, dayTitle: “Lame day at home..”, dayDescription: “Installed arch..”, hugeLoadOfIndividualSmallNotesForThisDayWithTimeStamps: [ { data: “Woke up, start now”, time: “9:44”, { data: “Finally figured out what fdisk is”, time: “21:29” } } … ] }​Example 3: My database has a collection for each day. (This is basically same as example 2, but there will be less collections. I am very unsure whether this would be bettter than option 2 performance wise, and also this would KIND of, be harder to implement because days change etc.), and each entry to that collection looks like this:​{userID: 902, dayTitle: “Lame day at home..”, dayDescription: “Installed arch..”, hugeLoadOfIndividualSmallNotesForThisDayWithTimeStamps: [ { data: “Woke up, start now”, time: “9:44”, { data: “Finally figured out what fdisk is”, time: “21:29” } } … ] }​As said before, thanks in advance guys!

Submitted October 20, 2019 at 01:29PM by livinglibary

No comments:

Post a Comment