Organising a database
Before we start saving and accessing data in our database, we need to understand how that data needs to be organised.
Every database product does this differently.
Some databases can be divided into tables. Each table has rows and columns. Every row is a different record, and each column describes a different aspect of that row.
MongoDB is a document-oriented database. When we use it, we should think of data in terms of seperate documents, rather than as rows and tables.
Documents
MongoDB stores data in documents.
For a blogging app, every blog post might be stored as a document like this:
{
title: "My blog post",
datePublished: "2018-08-01",
category: ["Programming", "Travel", "Confectionary"],
content: "This is the content of my blog post",
visible: true
}
This document stores four pieces of data about a blog post: title, publication date, category, content and visibility.
We can use any data type we like, including strings, numbers and booleans.
Arrays
This example also uses an array, a data type we haven't yet used in our app. Arrays are a data structure similar to lists.
Storing category as an array lets us attach a blog post to as many categories as are relevant, rather than forcing the user to pick just one.
The square brackets let us know that an array is being used. Each element (or list item) is separated from the next with a column.
Objects
The entire document is itself a data type called an object.
We've used objects several times before without referring to them by name. Wherever you see curly brackets, you're using an object.
Collections
A very simple app might only need to keep track of a single thing—posts, in this case.
But as our app becomes more complex, it might need to keep track of users, comments, categories, tags, menus and more.
Each of these could be represented as a document with its own format.
For example, a user might be represented like this:
{
username: "skywalkerl",
email: "l.skywalker@theforce.com",
role: "administrator",
password: "usethe4ce"
}
In this example, we've stored the password in a very insecure way. This would put our users at risk if our database was ever hacked. Never do this on a real app.
We can collect similar documents together.
MongoDB allows us to define collections of documents that all represent the same kind of thing.
We might have a "posts" collection and a "users" collection.
Each document inside the collection would represent a single post or a single user.
Models
Rules describing documents are called models.
Later in this module, we'll write the code to describe our app's data models, but it's worth thinking about how you'll need to store your app's data now.
The model for our "posts" collection above might look like this:
{
title: String,
datePublished: String,
category: Array,
content: String,
visible: Boolean
}
The model describes two things:
- Names for every piece of data held
- The data type of each
Each collection will have a different model. Every document in that collection will need to follow the rules in the model.
Part of Databases
- Introducing databases
- Organising a databaseP
- Using a database driverP
- Creating modelsP
- Making forms workP
- Collecting form dataP
- Saving form dataP