Adolfo Herrera
Adolfo Herrera

Reputation: 13

Correct structure for MongoDB

I am fairly new to mongoDB and databases in general and I am not sure what the correct/typical structure is for setting up different attributes.

For example, Lets say I have a person named Tim and he can play basketball, soccer, and tennis. How do you go about stating this? do you use booleans or store an array of strings?

This is how I think the format is..is this the correct way to think about it?

name: 'Tim',
sports: {
    soccer: true,
    tennis: true,
    basketball: true,
    football: false
}

Upvotes: 0

Views: 58

Answers (1)

Markus W Mahlberg
Markus W Mahlberg

Reputation: 20703

Data modeling in MongoDB works differently than with RDBMS. A typical workflow with RDBMs is that you define your entities and their properties as well as their relations and then bang your head against the wall to get your „upper left above and beyond“™ JOINs right so that the data gives you the answers you need.

The typical workflow for NoSQL databases in general and MongoDB in particular is different. You identify the questions you need to get answered by your data first, and model your data so that these questions can be answered in the most efficient way. Hence, let us play this through.

You obviously have people, for which sports they participate in should be recorded. So the first question we have to ask ourselves is wether we embed the data or wether we reference the data. Since it is rather unlikely that we hit the 16MB document limit, embedding seems to be a good choice. Now, with staying with an object holding Boolean values for every sport imaginable (later down the road), not only to we introduce unnecessary verbosity, we add exactly 0 informational value in comparison to holding an array of sports:

{
  Name: “Tim“,
  Sports:  [”Soccer”,“Tennis”,”Basketball”]
}

This makes it much easier to introduce new Sports later down the road (simply append said sport to the array of people doing it), does not pollute each and every document with irrelevant data and holds the same informational value.

Upvotes: 1

Related Questions