What NoSQL is in NoSQL
Topic 1 of 34 · 8 minFree lessonA relational database like PostgreSQL keeps data in tables, where every row has the same columns. That works well for a lot of data, but not all of it. A blog post may have three comments or three hundred, and a product may have a size, a color or neither. NoSQL is a name for databases that store data in other shapes than tables.
Four kinds of NoSQL database
NoSQL isn't one database. It's a family, and each kind fits a different job:
- Document databases, like MongoDB, store each item as a document: a bundle of named values, much like a JavaScript object.
- Key-value stores, like Redis, keep one value under each name, and answer very fast.
- Wide-column stores, like Cassandra, spread huge tables across many machines.
- Graph databases, like Neo4j, store things and the links between them, like friends in a network.
Most of this course is MongoDB, the most popular document database. Near the end, you'll try Redis too.
A document
Here's one book from a bookstore's database. You ask for it in MongoDB's shell, where you type commands, and the shell prints the result.
db.books.findOne({ title: "Dune" })A document is written between curly braces. Inside, each field has a name, a colon and a value. Text goes in quotes, numbers don't, and a list of values goes in square brackets. The ratings field even holds a smaller document inside it.
The same data in a table
In a SQL table, the two genres and the ratings wouldn't fit in one row. You'd need a separate table for genres, and more columns or another table for ratings. A document keeps everything about the book together, in one place.
This is the main idea of a document database: store data the way your app uses it. If the app shows a book with its genres and ratings, one document can hold them all.
Documents can differ
Documents in the same group don't need the same fields. This book is newer, so it has no ratings yet, and its document has no ratings field at all.
db.books.findOne({ title: "Circe" })In a table, every row would need a ratings column, empty or not. Here, a field is only there when it has a value.
How commands are written
Every command in this course starts with db, the database you're using. Next comes the group of documents, like books, and then what to do, like findOne. The braces after it describe which document you want.
db.books.findOne({ author: "Jane Austen" })Jane Austen wrote two books in this store, and findOne returns the first one it finds. You'll learn to find them all in a later topic.
db. MongoDB doesn't stop with an error. It looks in a group called book, which has nothing in it, and gives back null: "nothing found".db.book.findOne({ title: "Dune" })When you get nothing back and expected something, check the spelling first. The list under the editor shows every group of documents and its fields.
Try it yourself
Run the code in the editor and answer 8 practice questions on what nosql is. It's free, you only need an account.