0

I am working on a project where there will be different kinds of accounts that use Auth0 for auth and authz. I want to have it so that an account will query my backend to get back documents that pertain to that user. The issue is I want to be able to have multiple accounts set to "own" each document where the number of owners could be 1 to 5.

I have come up with two options on what I could do and I'd like help figuring out which option makes more sense or maybe another one I had not thought of.

Option 1: Store a list of the objectIDs in Auth0 user data and then query in a loop for each of the documents till I fetch them all - major con as multiple DB calls causing delays when potentially 10-50 documents needed to fetch

Option 2: Add an array to each document that contains that document's owner(s) Auth0 user ID and then query Mongo for all documents which contain that user's ID in the field of the document. - should be efficient but wondering if the correct place for the data to be stored it's sorta user data.

Option 3: Something totally else?

7
  • 2
    Neither. You need an associative entity a.k.a junction table. Commented Oct 7, 2021 at 2:00
  • @JohnWu: you are right that this is a 3rd option, but specificially for a NoSql DB, normalization is neither mandatory nor always "best practice" . And even for a relational DB, sometimes denormalization can make sense, Commented Oct 7, 2021 at 9:16
  • joshk132, I am not an expert for MongoDB, but I don't understand why your option 1 requires "multiple DB calls". Having a short look into the MongoDB docs, it seems one can execute a single query for a set of IDs using the $in operator, just I would do it in SQL with the IN operator. In SQL, something like `SELECT * FROM documents WHERE objectID IN(id1, id2, id3, ...)'. Please clarify. Commented Oct 7, 2021 at 9:24
  • @DocBrown huh didn't know the in operator worked that way. I had it in my head that it was option 2. Assuming I can use $in what option would be better 1 or 2? Commented Oct 7, 2021 at 20:36
  • 1
    @joshk132: yep, this happens often: people come here and ask our community what is "better", but that's often just a sign the asker hasn't thought through their own specific requirements of their specific use case. See also Why questions about "the correct way" are too broad Commented Oct 8, 2021 at 5:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.