02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Database, Collection, and Document in MongoDB

This tutorial introduces you to the core elements of MongoDB—databases, collections, and documents. These fundamental building blocks of MongoDB enable developers to structure and interact with data efficiently. Whether you are new to databases or transitioning from SQL, this guide provides a strong foundation.


What is a Database?

A database in MongoDB is a container for collections. It acts as a project-level bucket where all related data resides. Unlike SQL databases, MongoDB databases are created automatically when referenced for the first time. Simply type use <database_name> in the Mongo shell, and MongoDB will create and switch to that database if it doesn’t exist.


Collections: Equivalent to Tables

A collection is analogous to a table in SQL but is more flexible. It groups related data items together. Collections are also created implicitly when you insert data into them. For example:

db.myCollection.insert({ name: "Example Item" });

This command creates the myCollection and inserts a new document.


Documents: JSON-Like Objects

A document is the fundamental data unit in MongoDB, stored as a JSON-like object. Each document is a record with a unique _id field, ensuring easy indexing and access. Example of a document:

{
  "_id": "unique-id",
  "name": "Alice",
  "skills": ["JS", "Python", "MongoDB"]
}

Documents can store arrays, nested objects, and other rich data types.


Practical Tips

  1. Switch Between Databases: Use db to see the current database. Switch databases using use <database_name>.
  2. Insert Data: Use insert() to add a document to a collection. MongoDB automatically assigns a unique _id.
  3. Query Data: Use find() to retrieve documents. Chain .pretty() for formatted output.

Example:

db.myCollection.find().pretty();

Summary

MongoDB’s structure simplifies data handling through its JSON-based documents and dynamic schema. With just a few commands, you can create databases, organize collections, and manage rich data documents.

Ready to dive deeper? The next tutorial covers preparing data for MongoDB!

Video Transcript

00:01MongoDB, which is derived out
00:03of the NO SQL standard has three main types of data
00:07that you should expect.
00:09The one is database.
00:10Now if you work with databases before you have the server
00:13and then you have a database itself for the project itself.
00:16So a little, it's a little bit.
00:18DB database is a little bit confusing.
00:20But a database specifically is
00:22whenever you bundle up information per project, you want
00:25to put it inside of a database On contrary MongoDB
00:28as a server that could host many databases,
00:32your collection is similar to tables if you know
00:34what they are from SQL.
00:36But if you don't, a collection is basically a collection
00:39of related items that you put together within a project.
00:43So you'll have more than one collection usually
00:46within one database.
00:48Last but not least, is a document which is comparable
00:51to a row in SQL if you've worked with SQL previously.
00:55If not, in our construct,
00:58a document is literally just an object
01:00that stores information that lives within a collection
01:03and collections live within a database.
01:06Beyond that, that is really all you need
01:08to know from the core basic terms.
01:10With that said, let's see them in action.
01:12Let's see how you work with them in Mongo db.
01:16So I'm assuming at this stage
01:18that you have Mongo DB installed
01:20and you have the Mongo D server running
01:23and you're inside the Mongo shell.
01:26It doesn't really matter what the current version
01:28that you're in, although if you wanna know
01:30what is the current version, by the way, oopsie,
01:34if you wanna know what the current version is,
01:35you could just type DB version open
01:39and close from inside there
01:41and you'll see that I'm currently running on MongoDB version
01:443.26.
01:46Now with that said, I wanna show you the basics of Mongo db
01:50and I'll give you also a side note that
01:53we are not gonna go into every single detail in MongoDB,
01:56but I'm gonna show you the basic foundations of it.
01:58And by the way, it doesn't really matter if you worked
02:00with a database before or not really, doesn't matter.
02:04So let's jump right into it. The first thing is databases.
02:08They store information that we provide them.
02:11In the case of MongoDB, it stores this information in JSON,
02:16uh, basically in JavaScript.
02:17So it's very, very easy to work with.
02:19If you know JavaScript.
02:22Now, generally speaking, a database
02:24or like Mongo D has many databases.
02:28Now the way you know
02:29what database you're in currently is just by typing db.
02:33And when you type db, it will output
02:35and tell you that you're currently in whatever database.
02:38Now, if you didn't set or configure a database, by default,
02:41it's gonna go to the test database.
02:44Now, contrary to traditional database structures
02:48where you create a structure
02:49and you set it up in advance with MongoDB
02:53and no SQL in general, which is the name of the type
02:56of database that MongoDB implements,
02:59It's called no SQL, because there's no SQL in it.
03:03The way you change your database is by asking to use
03:07and then you define the name
03:08of the database you wanna work with.
03:11For example, I wanna work with the new database.
03:14Now automatically what happens is Mongo checks
03:17to see if Mongo if new exists,
03:19and if it doesn't, it creates that database
03:22and switches into it.
03:23And now if I type db, we'll see that I'm inside, not bd,
03:27but db, we'll see that we're inside of New.
03:30Now lemme just double check if Clear will clear things out,
03:33CLS that, there we go.
03:34CLS will clear our screen.
03:36Okay, so now that we know how to use a specific db, we saw
03:40what DB we're in.
03:41We saw that we could use different DBS by switching,
03:44by typing the word use
03:46or switching back into that new database.
03:50The next step, every single database in MongoDB
03:53and no SQL holds collections.
03:56Now you don't explicitly create collections,
03:59but instead of that, just like with actual creation
04:02of databases, you approach the database.
04:05In this case, our database is new
04:08and in IT I'm gonna ask from db, which is the, the name
04:11for it dot the name of the collection I want to create.
04:15So in this case, let's say I want
04:16to create a chart collection.
04:19And this chart collection has been created once I
04:23and I, I don't, I never create it explicitly,
04:25but instead of that I insert into IT documents,
04:28which is the name of what we insert into them
04:31by typing the word insert.
04:34Now the what I put inside is objects.
04:37So for example, name Ben.
04:40Now notice how I'm literally just typing standard
04:42JavaScript objects.
04:44That is really what I put in.
04:45But to be very exact, we're putting in JSON.
04:47So technically speaking, I need to put here,
04:50obviously this has to open and close.
04:52Technically I should put here and open and close as well.
04:55But MongoDB is forgiving enough that if you send
04:59standard objects inside,
05:00it will process them and accept them.
05:02And in this case, I've just inserted a document.
05:06We call these objects documents inside of MongoDB,
05:09I've inserted a document into a collection
05:13inside of our database.
05:15So those are the three key terms that you need
05:17to know when you're working with no SQL
05:19and specifically MongoDB.
05:21You need to be familiar with the database,
05:24the collection, and then the object.
05:27And that's exactly what we've done.
05:29Now, last but not least, by the way, if you want to fetch
05:32that information, one way to do that is we're gonna go to
05:34that collection and we're gonna find all of the items
05:38inside of that collection.
05:40And if I want to, I could also make it pretty
05:42by typing dot pretty,
05:44and that's all I need to do,
05:46just a regular JavaScript line of code.
05:48And what will happen is gonna output our object.
05:51And by the way, automatically when you place something
05:54inside, it will automatically add a underscore ID
05:58and give it a unique id.
05:59So every item has a unique id,
06:01so it's easily accessible and index.
06:04By the way, this is only true
06:06for the first level of an object.
06:08So for example, if I'm gonna add here, by the way,
06:10if I add another Ben, it's not unique, right?
06:12So the way we make it unique is
06:14that MongoDB automatically assigns a unique ID
06:17to each one of these items.
06:18And now first Ben
06:19and second Ben are completely different objects.
06:22Moreover than that, I could add in more complex things such
06:25as, let's say Ben and skills.
06:30And I could put here an array.
06:33The only thing that matters is that it's a, a legal,
06:35A skill is uh, JS and XML, let's say,
06:39and PHP, let's say.
06:42And as long as I put valid JS on
06:45or more accurately valid
06:47objects, then it should come through.
06:48But if I put here something that's not valid, for example,
06:50this PHP is not closed, then I'm gonna get an error.
06:54It's not gonna actually complete, it's not gonna understand
06:56what I actually wanted to do.
06:57So I have to make sure that I'm always typing in everything
07:00validly and everything correct.
07:02And there we go. And I've just added
07:03another item, another Ben.
07:05Now the, the, those unique IDs that are placed
07:09or only placed on the root document, excuse me.
07:12While for doing that on the root document itself,
07:15it is not placed within child elements.
07:18So skills array doesn't get, uh, any object id.
07:21So the object ID is only on items that are placed directly
07:24to live within a collection.
07:26So that's it. Those are the basics of Mongo db.
07:34In this lecture, we got familiar to
07:36with the terms database collection and document.
07:40In the next lecture, we'll go ahead
07:41and prepare our data for man Mongo db.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!