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

Preparing Data for MongoDB: Step-by-Step Guide

In this tutorial, we take the next step in working with MongoDB by preparing, structuring, and inserting data into a database. Whether you’re organizing datasets for analysis or building an application, this guide will walk you through creating documents and managing them in MongoDB collections.


Creating the Data Structure

Data preparation begins by defining a JSON object representing your dataset. For example, if you are working with student scores, create an object that includes labels for subjects and datasets for individual students. Each dataset can include names, scores, and any additional properties like colors for visualization.

Example data structure:

{
  "labels": ["Math", "English", "History", "Science"],
  "datasets": [
    {
      "name": "James",
      "data": [93, 88, 99, 92]
    },
    {
      "name": "Larry",
      "data": [70, 65, 80, 75]
    }
  ]
}

Inserting Data into MongoDB

Once the data is prepared, the next step is to insert it into MongoDB. Use the following steps:

  1. Switch to the desired database:
    use chartsDB;
    
  2. Insert the object into a collection:
    db.charts.insert({
        "labels": [...],
        "datasets": [...]
    });
    

MongoDB will automatically generate a unique _id for the document, making it easily retrievable later.


Fetching and Using Document IDs

To retrieve the data and its unique identifier, use the find command:

db.charts.find().pretty();

This command outputs all documents in the collection. Copy the _id of the inserted document for reference in future operations, such as creating a server or retrieving data dynamically.


Summary

This tutorial demonstrated how to create structured data, insert it into a MongoDB collection, and retrieve the document ID for later use. These foundational skills are essential for any application that relies on dynamic data storage and retrieval.

Next, we’ll set up a Node.js server with Express and MongoJS to connect to our MongoDB database and leverage this data for application development!

Video Transcript

00:01All right.
00:01So now that we know the basics of how to work with MongoDB,
00:04it's time for us to create our data state
00:06that we'll be sending into MongoDB.
00:09Once we have it ready, we'll insert
00:11that data into the database we're gonna insert
00:14to be accurate, we're gonna insert our document
00:16into a collection within a database,
00:19and then we're gonna also fetch the ID of our data.
00:23So we'll be able to fetch it in the next lecture.
00:26So let's see how we set that up in this lecture.
00:32Okay, so we're familiar with MongoDB
00:35and we've seen basically how to work with it.
00:38And before we continue onwards, the next thing I want
00:42to do is create the data source that we're gonna be using
00:45inside of MongoDB.
00:47So to do that, I'm just gonna go into an
00:49any empty area in an editor.
00:52And what I'm gonna do is I'm gonna create
00:53with you just a data object, and it's gonna go this way.
00:58So our data objects,
00:59and by the way, we're in the process of creating
01:02a more complex data structure.
01:04Now our data structure is gonna have basically a few labels,
01:10kind of similar to what we had previously.
01:12Now I'm, I'm just gonna go ahead
01:13and copy a what a, a pile of labels
01:16that have pre-configured and pre-created.
01:19And I'm just gonna walk you through them.
01:21I have math, English, Spanish history, philosophy,
01:24sports, and science.
01:26Beautiful. Let's continue.
01:28So lemme just GOs a word wrap as I'm continuing.
01:31The next set step is, Anita, set my data sets,
01:35and I'm gonna have two data sets.
01:37Now, this time around my, my data sets are gonna be split
01:40among different people.
01:41So person number one will have one dataset.
01:45Person number two will have another dataset.
01:47So our first person will call him James.
01:51Not Hames, not Hames, not hames. Okay.
01:56I was, that was, I was stuck in a loop.
01:58Um, now the next few things are really just about colors.
02:01So for example, I, I already pre-configured a few colors,
02:04so I'm just gonna slap them right in there.
02:06So we, I'm setting here a ba,
02:07a background color within a 20% opacity.
02:10I'm setting oto a border color.
02:12And by the way, I'm, I've kind of like grabbed these colors
02:15directly from the sample in, um, chart js,
02:20just for the sake of keeping things simple.
02:23And I'm just gonna go ahead and just copy all these colors.
02:26All this stuff is not mandatory.
02:27You could put whatever colors you want instead,
02:30but in this case, I'm just gonna use some default colors
02:33that seem to be working well.
02:35Why be creative if you don't have to be in this site?
02:39Okay, so now that we have James,
02:40and let's set up his data points.
02:42So for his data, I'm literally just gonna put in directly
02:45the content itself
02:46and just gonna give the scores he got in the different, uh,
02:49well, James is an incredibly smart student
02:51and all of his scores are 80 or above.
02:54So, uh, in math, he got 93 in English. He got 88 in
02:58Spanish, he got 93 in history, he got 99.
03:02Wow, that's impressive philosophy. He's a great philosopher.
03:06Um, sports, eh, not the greatest sportsman,
03:09but eh, let, let's give him a low sports.
03:12And for science, he is definitely into science there.
03:14He got 92. Beautiful. So that's our data source for James.
03:19Lemme go ahead and just duplicate this
03:21and we're gonna create another data
03:22source for another person.
03:24How about we'll do this time around.
03:27Let's set it to be Larry.
03:30And for Larry, let's go ahead and change a little bit.
03:32The colors here, so I'm just gonna go ahead
03:34and grab that color and just push it right there.
03:38And I'm just gonna do the exact same thing over here.
03:42So we're gonna get a different color construct.
03:45Oopsie, no, let me just copy this whole thing.
03:49Paste that here, paste that here, paste that here.
03:53So it's all the same.
03:55Oh, we don't, do we want it to be the same color?
03:58Yeah, why not? Let's have it the same color. Okay.
04:01So that's basically our structure.
04:03And let me go ahead and I'm just gonna move a few
04:05of the scores, but
04:07how about we'll make it a little bit worse
04:08so he is not as good.
04:10And so things and 70.
04:14And let's give them just like 10 here.
04:17And maybe their scores are same in that one,
04:20but they're not in this one.
04:21Beautiful. So we have two data,
04:23data entries and there we go.
04:24That's it. That is our data set.
04:26So the next step that we want to do is I'm gonna go ahead
04:29and what I want to do is I want to capture,
04:31and by the way, if this was a real data,
04:33we really don't need the variable name.
04:35'cause what, what I really want is to grab this object
04:38that we've just created and I'm just gonna cut it
04:41and I'm gonna go right into our MongoDB driver.
04:46And here I'm gonna go ahead.
04:47First of all, I want to use a certain database.
04:49Let's give a name to the database that we want to use.
04:52So let's say, we'll call this charts db.
04:55That's gonna be our database for charts.
04:58Now that we're in charts db, I'm gonna go to that DB
05:01and I'm gonna go to charts and I'm gonna input a new chart.
05:04So I'm gonna insert a new chart by literally
05:07what I'm gonna do is I'm gonna paste that full object,
05:10close a round bracket, hit enter.
05:13And what I've just done is I've just added that item, CLS,
05:18I've just added that item into our charts.
05:21So let's go ahead and see it.
05:22So what I'm gonna do is I'm gonna click here.
05:24Charts do find, and I'm just gonna find it all.
05:28I don't even need to make it pretty.
05:29What I'm really interested,
05:31and you see this object ID element here, I'm just gonna copy
05:34that and put it right into our server here.
05:38We're gonna talk about the server in the next lecture,
05:40but I'm just gonna piece it here and just keep
05:41that reference, copy it
05:43because we're gonna need it for the next lecture.
05:47In this lecture, we inputted our data into our database
05:50and got out of it an ID for our document,
05:53which we'll use once we move into creating our server,
05:56which we surprisingly are gonna be doing in the next
06:00lecture.

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!