‫Just like in the previous sections,
‫before we dive into a new technology,
‫let's actually learn what it's all about.
‫So in this case, let's learn what MongoDB actually is,
‫how it works, and a quick overview
‫of how it compares to more traditional databases.
‫And let's begin with a simple overview.
‫So MongoDB is obviously a database,
‫and it's a so-called NoSQL database.
‫Now some people also say No S Q L,
‫but I'm just gonna keep saying "no sequel", all right?
‫Now, the other type of database, which is
‫sort of more traditional, is the relational database,
‫which NoSQL is often compared to.
‫Anyway, in Mongo, which we can also say instead of MongoDB,
‫each database can contain one or more collections.
‫So if you actually are coming from one of
‫these more traditional relational database systems,
‫you can think of a collection as a table of data.
‫Then, each collection can contain one
‫or more data structures called documents,
‫and again, in a relational database,
‫a document would be a row in a table.
‫So each document contains the data
‫about one single entity, for example,
‫one blog post or one user or one review,
‫or anything else, really.
‫You get the point, right?
‫Now the collection is like the parent structure
‫that contains all these entities.
‫For example, a blog collection for all posts,
‫a users collection or a reviews collection.
‫And you can also see here that the document
‫has a data format that looks a lot like JSON,
‫which will make our work a lot easier
‫when we start dealing with these documents.
‫And of course we will talk a lot about this later,
‫but for now on, let's learn about Mongo's main features.
‫So, according to MongoDB's website,
‫MongoDB is a document database with the scalability
‫and flexibility that you want,
‫and with the querying and indexing that you need.
‫Now, that sounds a bit over the top,
‫so let's try to understand what this actually means.
‫So, as we saw before, MongoDB is a document-based database,
‫so it stores data in documents which
‫are field-value paired data structures like JSON.
‫So again, it stores data in these document
‫instead of rows in a table
‫like in traditional relational databases.
‫It's therefore a NoSQL database
‫and not a relational one.
‫Also, MongoDB has built-in scalability,
‫making it very easy to distribute data
‫across multiple machines as your apps
‫get more and more users
‫and starts generating a ton of data.
‫So whatever you do, MongoDB will make it very easy
‫for you to grow.
‫Next up, another big feature of MongoDB
‫is its great flexibility.
‫So there is no need to define a document data schema
‫before filling it with data,
‫meaning that each document can have a different number
‫and type of fields.
‫And we can also change these fields all the time.
‫And all this is really in line
‫with some real-world business situations,
‫and therefore can become pretty useful.
‫MongoDB is also a very performant database system.
‫Thanks to features like embedded data models,
‫indexing, sharding, the flexible documents
‫that we already talked about, native duplication
‫and so much more.
‫And you don't need to know all of this, of course,
‫but it's sure nice to know that MongoDB is highly performant
‫if we need it to be.
‫Finally, I just wanted to add that MongoDB
‫is a free and open-source database,
‫published under the SSPL license.
‫So in summary, we can say that MongoDB
‫is a great database system
‫to build many types of modern, scalable
‫and flexible web applications.
‫And in fact, Mongo is probably the most used database
‫with no JS, and so it's a perfect fit
‫for us to use in this course.
‫Okay, now let's talk a bit deeper
‫about these documents, and returning
‫to our blog posts example from the beginning,
‫this could be a very simple representation
‫of a single post document, right?
‫And now just for the sake of comparison,
‫here is how that exact same data
‫could look like as a row in a relational database
‫like MySQL, or even in an Excel spreadsheet,
‫if you're more used to that.
‫So as I mentioned a bit earlier,
‫MongoDB uses a data format similar to JSON
‫for data storage called BSON.
‫It looks basically the same as JSON,
‫but it's typed, meaning that all values
‫will have a data type such as string, Boolean,
‫date and teacher, double object or more.
‫We will learn all about this later in practice.
‫So what this means is that all MongoDB documents
‫will actually be typed,
‫which is different from JSON, all right?
‫Now just like JSON, these BSON documents
‫will also have fields and data is stored
‫in key value pairs.
‫On the other hand, in a relational database,
‫each field is called a column.
‫So here again you can see how
‫these databases arrange data in table structures
‫while our JSON data is so much more flexible.
‫Take for example the tags field,
‫where we actually have an array,
‫so we have basically multiple values for one field, right?
‫So MongoDB, space and DV in this case.
‫But in relational databases, that's not really allowed.
‫We cannot have multiple values in one field,
‫and so we would actually have to find workarounds for this
‫which could then involve more work
‫and more overall complication.
‫Now another extremely important feature in MongoDB
‫is the concept of embedded documents,
‫which is, again, something not present
‫in relational databases.
‫So in our comments field here,
‫we have an array which contains three objects.
‫One for each document,
‫and each of them could actually be
‫its own document, right?
‫So just imagine we had a comments collection
‫which contained a bunch of comment documents.
‫Each of them could actually look exactly like this,
‫so with an author and with the comment text,
‫but instead of doing that,
‫we include these comments right into the blog post document,
‫so in other words, we embed the comment documents
‫right into the post document.
‫So this process of embedding, or de-normalizing
‫as we can also call it, is basically to include,
‫so, to embed, some related data all
‫into one single document.
‫In this example, the comments are related to the post,
‫and so they are included in the same document.
‫And this makes a database more performant
‫in some situations because this way,
‫it can be easier to read all
‫the data that we need all at once.
‫And this is something that we're gonna talk about a lot
‫when learning about data modeling,
‫but for now, I hope that this still makes sense to you.
‫Now, the opposite of embedding or de-normalizing,
‫is normalizing, and that's how the data is always modeled
‫in relational databases.
‫So in that case, it's not possible to embed data,
‫and so the solution is to create
‫a whole new table for the comments
‫and then join the tables by referencing
‫to the ID field of the comments table.
‫Now we're not gonna use relational databases
‫in this course, but I believe it's still important
‫to know the differences if you wanna become
‫a good back-end developer.
‫Anyway, and now just to finish,
‫two more things about BSON documents.
‫First, the maximum size for each document
‫is currently 16 MB, but this might increase in the future.
‫And second, each document contains a unique ID,
‫which acts as a primary key of that document.
‫It's automatically generated with the object ID data type
‫each time there is a new document,
‫and so we don't have to worry about it.
‫All right, and that should be a brief enough overview
‫to get us started,
‫and to actually use MongoDB from the next lecture on.
‫So, let's move on now.