Sunday, 20 September 2020

Adding a document to a document in MongoDB

So far this community has helped me out a lot in my struggles as a Node noob. Thanks for that!So here I am again with another question. Currently I am working on a webapp that let's users login and add tasks to a task list. To save the users and tasks I am using MongoDB. Currently there is only one main tasklist that is accesible by every user, this means that a user doens't have it's own tasklist. I want to implement a feature where every user DOES have their own tasklist.The first way to do this, I guess, is to let every user have a task attribute in the database, where I store their own tasks. So if I want a users tasks I grab their user document in the database and look for the task attribute.The second way I came up with would mean that I store all tasks in a seperate task collection and let every task have a userID attribute. Then I can search through this collection using the users id to find all their tasks. But this would mean that I have to search through all the tasks, this would be very ineffiecient I guess.Is the first way the right way to do it or do you suggest another way of doing this? And if the first way is the right way, how would I implement this?Thanks in advance.

Submitted September 20, 2020 at 06:05PM by rubennebur

No comments:

Post a Comment