Video Transcript (English)
Welcome back! Now that we understand the basics of the MongoDB document model and the data types it supports, let’s start working with some data.
MongoDB Atlas makes it easy to interact with and manage your data within the Atlas UI.
In this video, we’ll use the Atlas UI to navigate our data, add documents to a collection, and perform a basic query.
Let’s get to work.
We’ll begin by viewing the databases in our Atlas cluster. If you completed our unit on getting started with MongoDB, then these initial steps will look familiar to you.
We select “Database” to see all database deployments in our current project.
For this demonstration, we’ll use an M0 cluster.
To view our data in Cluster0, we select Browse Collections, so we can view our data.
We are directed to the collections tab, which gives us a list of our existing databases.
Let’s find our movies database, sample_mflix, by using the search bar.
Note that if you want to return to the full list, simply clear the search bar.
We can expand each database to see the collections within it.
In this case, our sample_mflix database contains multiple collections, such as ‘comments', 'movies', and 'users'.
Let’s take a closer look at the movies collection.
When we click the collection name, we see some metadata about the collection at the top, such as the size of the data in kilobytes and the number of documents.
Documents within the collection are displayed on the right side. There are some sample documents with a detailed view of each document’s fields and values.
Fields containing arrays or nested documents are marked with an expand arrow.
For example, if we click the arrow next to the `cast` field, we’ll see an array of string values.
Now let’s expand the tomatoes field to see an example of an embedded document.
The 'tomatoes' field is an object that contains additional fields and objects. If we expand the viewer object, we get the number of reviews, the rating, and the freshness meter value, which is a rating unique to this website.
So far we’ve viewed how to navigate our databases, collections, and documents using Atlas. Now let's talk about how to create them.
Our mflix movie service has decided to expand and start selling merchandise. We want to sell the usual SWAG, which are promotional items such as hats and t-shirts. But, we also want to start selling collectible memorabilia from the films. We need a database and collections to track this.
To create a new database, we click the 'Create Database' button.
Now we’re prompted to enter a name for our new database. Let’s name it 'mflix_merchandise'.
We also need to name the first collection in this new database. We'll name this one 'swag'.
The next dropdown is for additional preferences, which are out of scope for this lesson. For more information on these, check out our MongoDB Indexes II unit.
For this example, we’ll just create a regular database without those additional options, so let's go ahead and click the “Create” button.
Now that we’ve created our database and first collection, we are redirected back to the Collections tab. The new mflix_merchandise database is listed along with the others and we are automatically on the swag collection. Nice!
Now we need a collection for memorabilia.
We click the mflix_merchandise database to view its collections.
Then we can add a collection by clicking the plus button next to mflix_merchandise, or by clicking the 'Create Collection' button. Let’s use the Create Collection button.
Now we name the collection - we’ll call it ‘memorabilia’.
Now we click create.
Great! We have a database and collections, so the next step is to insert some documents.
First, we select the 'memorabilia' collection to view and manage its documents.
To add a new document, we click the 'Insert Document' button.
As we can see here, we have the option to insert our data as JSON or to use a table view. We'll use the table view for this example to show how to select data types using Atlas.
The first document that we will input is for an autographed photo from the movie 'The Alamo'.
We need to enter fields for type of merchandise, whether it’s authenticated, a description, who signed it, and its value.
To add the first field to the document, we hover to the left of the '_id' field and click the plus button.
This brings up the option to Add field after_id. We click that, and then enter our first field and value, which is type and photo.
Notice that the value is automatically set as a string. That is fine for this field, but we’ll need to adjust it for some of our other fields.
Now let’s add another field for is_authenticated. For this field we need to click String and select the boolean type, because the value will be true or false.
We follow the same steps for both the description and value fields, at which point our document looks like this.
Next is the signed_by field, which is an array because three people autographed the photo. So select the Array data type.
This automatically creates the field key for the first element in the array. Then we can enter the data, proceeding in the same way as before.
John Wayne
Richard Widmark
Now we click the 'Insert' button to save the document.
Awesome! Our document is now part of the 'memorabilia' collection.
Querying in Atlas is just as easy. Let’s run a simple query to demonstrate, using our movies collection.
We can see here that there are 21,349 total movie documents in this collection, but how many of those have John Wayne in them?
Let’s find out.
For a simple query, we can enter field and value pairs directly into the filter bar. When we click in the filter bar, two curly braces appear. Let's use the cast field to filter John Wayne movies.
We can either press Enter or click the Apply button to show only those that match our query.
Awesome! We found the movies with John Wayne in them!
Only the first 20 results will appear on this page. To iterate through the rest, we click the Next button.
Nice job! In this video, you learned how to use Atlas to explore your database, collections, and documents.
We created collections, inserted a document, and ran a simple query in the Atlas UI.
With this foundation, you’re ready to use Atlas to manage your data, and to leverage its functionality in your projects. As a next step, try inserting your own document into a collection in Atlas.
Now that you have a handle on using Atlas to interact with collections and documents, let’s take things a step further. In the next lesson, we’ll begin to talk about data modeling. It will be important for you to begin to build a foundation in this topic as you become more familiar with the document model. See you soon.
