Is this just brilliant or really, really stupid?

I’m having doubts about my way of handling this scenario. Please help me out before I screw up :slight_smile:

Basically, I have three concepts.

Random Questions

For example “What’s your favorite food?”

  • User of my app can create as many different questions as they like
  • The question can also pre-determine possible answers, or not.
  • There’s also a unique label for each question, so for this it could be “Favorite food”.

Answers by random persons

Simple strings

  • from real people, so an answer could be “Cheese macaroni”.
  • User wants to see what different persons have answered to each question

Person

  • They hold the answers to these questions.

I achieved this by simply using the question’s label as they “key” in an Answers object, a property of a person.

Code:

Question {
  label: String, // "Favorite food"
  text: String, // "What's the thing you like to eat?"
  options: null || Array, // null or ["Spaghetti", "Veggies", "Hamburgers"]
}

Person {
  name: String, // "John Doe"
  answers: Object // { "Favorite food": "Hamburgers", "Age": "I ain't telling you!" }
}

This was what I came up with, when I tried to find the leanest and the meanest solution. I know I can’t put anything into the answers-object’s key, so I’ve restricted the content of Question’s label to only alphanumeric characters with white space and parenthesis.

Is this a really, really stupid idea?

(I’m coming from a SQL background, so forgive me if the question is absurd or naive)

It looks fine, but I can guess that sooner or later people will be bothered by labels being already taken.

Now the possible alternatives.

If you change your schema to:

answers: Array // [{"questionId": "qwerty", "question": "What's the thing you like to eat?", "answer": "Hamburgers"},{},{},{}]

You won’t have the issue of not being able to use characters readers would want to use in the question.

As we’re in NoSQL world, you can also reference the same way in the Question object. Personally I’d use both at the same time.

answers: Array // [{"userId": "qwertyuio", "username": "Gusto", "answer": "I ain't telling you!"}, {}, {}, {}]

You can ommit the ids if you don’t plan to let users edit their questions or names, or you can use your labels instead of questionId.

Thanks! I understood your idea of using the id of the question in an object representing the answer. This (in my own thinking) had the following drawbacks:

  • It becomes (in theory at least) possible to have multiple answers per question (which I did not want)
  • Making sure there is only one answer per question increases amount of code needed and possibility of errors
  • More complicated to implement in other parts of my app where the data is used

Labels are “taken” only per-user, so separate users can indeed have the same questions and they will not know of each other. I over simplified the pseudo code, both the Question and Person -classes do also have a ownerId-property. So one user of my app has a bunch of questions which he/she can make up, and a bunch of people (persons) answering these questions. They need not worry about anybody else’s questions or answers.

However, this I did not understand:

As we’re in NoSQL world, you can also reference the same way in the Question object. Personally I’d use both at the same time.

Could you elaborate? Thanks! :slight_smile:

When user B answer the question of user A, you’d write the answer in both question object and answer object. This way you can just grab all the answers to particular question in a simple query, or all the answers of particular user also in a single query, without having them to reference each other, which will make your publications less expensive.

In SQL world such multiplying of data is a bad practice, but not in NoSQL. That’s what I meant.

All of the questions and suggestions can be valid or not depending on who is the Person object referring to? The author of the answer or the author of the question? I originally assumed it’s the author of the answer, so I wrote the questions based on this assumption.

Then from the user perspective, I’d see no point in having to provide a label. I mean, what would the user gain by having to word his question twice? What additional role will the labels have?

If something is a gain only for the codebase and not for the actual user experience, it should be server-generated instead of asking the user for additional interaction.

It becomes (in theory at least) possible to have multiple answers per question (which I did not want)
(…)
Also, if labels are taken only per-user and separate users can indeed have the same questions and they will not know each other.

What about same question by different users? With your schema, if I answer a “favorite food” question for one user, I won’t be able to answer the same question for different user.

Or did I misunderstand something?

I think the gotchas that you misunderstood are:

  • Questions are created by Users, who have an account in my app
  • Answers are provided by seemingly random or imaginary “people”, who don’t have an account (they are not users), but are instead represented in the app as “Persons”. They have a name and possibly some other info, like address. They can provide answers by filling up a form created by our app and shared via special link, for example
  • Each person is linked to only one user, and therefore can only answer one user’s questions. There is no overlap.

Then from the user perspective, I’d see no point in having to provide a label. I mean, what would the user gain by having to word his question twice? What additional role will the labels have?

–> The purpose of the label property was to act as a header/label in a table view when browsing the answers

edit: Also, the User might wish to just create a data field with a label and fill the data by himself, so there wouldn’t be a need to phrase a proper question. But sometimes, or most of the time, another person will fill in the data, so the question has to make sense.

1 Like

I think I get it now. So as a registered user, I keep the track of my questions, but I don’t keep the track of my answers. The real world use case would be f.e. marketing company A gathering answers from a lot of unregistered people for questions by registered representative of company B. This way the registered representave of company B will be only asking the questions, he’s not interested in answering questions of other people.

More like…

I’m a user of the app. I’m having a party. I want to know all the food allergies of people attending. I create two questions:

{
  label: 'Allergies',
  text: 'Are you lethally allergic to any food substance?',
  options: null // Allow them to specify
}, {
  label: 'Attending',
  text: 'Are you coming or not?',
  options: [ 'yes', 'no' ]
}

Then, I would pass around a special link https://myapp.com/question/1, which would open up a form asking them:

  • Name
  • Are you lethally allergic to any food substance?
  • Are you coming or not?

And as a result, I would have bunch of Persons attached to my User account, like this:

{
 ownerId: 'myUserIdString',
 name: 'John Doe',
 answers: {
   "Allergies": "No peanuts and shrimps"
   "Attending": "yes"
 }
}