0

I am new to Firebase and NoSQL and come from an SQL background. I was wondering what would be the best way to store my data:

There are 4 categories, and within each category, there are 3 sub-categories. For each subcategory, there is a list of around 10-20 questions that I need to be able to select 5 from each time the user requests it (at random).

I was originally thinking of just transferring the SQL design into NoSQL however I'm not sure if this just defeats the purpose of NoSQL

I was wondering if a solution like this would work, by storing a list of the 10-20 questions under each category under the subcategory:

"questions": {
    "category_one": {
        "subcategory_one": ["question_one", "question_two",...],
        "subcategory_two": ["question_one", "question_two",...],
        "subcategory_three": ["question_one", "question_two",...],
    },
    "category_two": {
        "subcategory_one": ["question_one", "question_two",...],
        "subcategory_two": ["question_one", "question_two",...],
        "subcategory_three": ["question_one", "question_two",...],
    },
    .
    .
}

Would storing it this way allow me to efficiently query and retrieve the data I need?

The first way I was thinking is like an SQL database where each question is its own JSON object.

"1": {
    "category": "1",
    "subcategory": "1",
    "question": "...",
},
"2": {
    "category": "1",
    "subcategory": "1",
    "question": "...",
},

(I'd assume this way is worse...?)

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Sophie
  • 1

1 Answers1

0

According to your last comment:

A minimum of 8, maximum of around 20.

If you'll always have a number of questions between 8 and 20 under a specific category and subcategory, then you have to create a reference that points exactly to the category/subcategory from where you want to read the questions:

db.child("questions").child("category_one").child("subcategory_one");

Now to get 5 random questions, then I recommend you use the solution in the following post:

How to get unique random product in node Firebase?

And to answer your question:

Would storing it this way allow me to efficiently query and retrieve the data I need?

Yes, that's a structure that can be queried to return the desired results.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I currently have all my data (the questions) in an Excel document, how can I efficiently import the data into a Firestore database? – Sophie Apr 03 '23 at 09:38
  • That's a totally different question. For that, I recommend posting a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Apr 03 '23 at 10:03
  • Based on the example structure I wrote, would the 'subcategory' be classified as a field or a collection? – Sophie Apr 04 '23 at 05:39
  • In the Realtime Database, there are no collections, there are just nodes. You can use the `subcategory_one` as a child of the `category_one` node, as I mentioned in my answer, or you can add it as a field and perform a query like this `db.child("questions").child("category_one").orderByChild("subcategory").equalTo("subcategory_one")`. It's up to you to decide which solution works better for your use case. – Alex Mamo Apr 04 '23 at 06:26
  • So regarding this question, can I help you with other information? – Alex Mamo Apr 05 '23 at 12:30