Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Mongo Basics Getting Started With MongoDB Documents in Mongo, where the magic happens

Javier Alvarado
Javier Alvarado
16,060 Points

What are the benefits of storing name as one object?

In the first example of a document, the first name and last name are stored as two separate fields. In the second example, they are stored as fields in a single name object. Is one of these methods for storing names preferred? What would be the benefits of of storing the names as one object versus separate fields?

2 Answers

Steven Parker
Steven Parker
231,271 Points

In most cases, storing the names separately would be preferable.

As Mark pointed out, you can always combine the names when needed for a particular use, but storing them separately gives you the most flexibility for displaying and/or searching through the data.

Even if the immediate needs do not require this capability, it might still be worthwhile so that the database can easily be made to accommodate additional needs in the future.

It is a matter of usage.

Let say you have a program that accesses the DB and in particular your table to gather information to help a person prepare his or her tax form. On the tax form, you need first name and last name as 2 separate attribute. In this case, the main consumer of your DB (the tax preparation program) is expecting a first name field and a last name field ... separate.

Now let say that the main consumer of your DB, and in particular you table, is a program that prints invites to close friends. The invite letter starts with with something like this:

Hello John Doe

we love to invite you to .............. and so on and so on

In this second case, the main table consumer (your invite letter program), require a single field for first name and last name.

sure you can concatenate and split as needed ... but this is beside the point. the table design is generally driven by the consumer of the table.

Hope this helps.