0:01 Hey guys, in this video we're going to 0:02 be covering MongoDB in depth. And most 0:06 of the content from this video is part 0:08 of a 10 project course in MongoDB that I 0:11 did. So if you enjoy it, I would suggest 0:14 checking that course out too, and I'll 0:16 leave a link in the description. So 0:19 we're not going to be building a web 0:20 application or anything like that using 0:22 other technologies. I really want to 0:24 just focus on MongoDB and basically the 0:27 syntax, how to create collections, 0:30 insert documents, update and delete 0:32 documents, create users, and some of the 0:35 other fundamentals of 0:37 MongoDB. All right, so is what's 0:40 called a NoSQL database. And there's 0:43 different types of NoSQL databases. So 0:45 to be more specific, it's a called a 0:48 document database. All right, data or 0:50 records are stored as documents and they 0:54 use JSON-like syntax. So if you don't 0:56 know what JSON is, it's JavaScript 0:58 object notation. And I'll get into the 1:01 syntax in a minute. So NoSQL databases 1:04 are much different than relational 1:06 databases like say MySQL or Postgres. 1:10 With a relational database, you really 1:12 have to map out everything. You need to 1:14 figure out the exact schema including 1:16 what tables you use, what fields you'll 1:19 use, and even the types of fields, okay? 1:21 Whether it's going to be a string or an 1:23 integer, uh, all that stuff. So, with 1:25 NoSQL, there's there's not really any of 1:28 that. I mean, you should plan out the 1:30 structure of your database and your 1:32 collections, but you don't have to do 1:34 any predefined structuring before you 1:36 build your application. All right. One 1:38 of the huge advantages to MongoDB and 1:41 NoSQL databases in general is scaling. 1:44 Okay, they're really easy to scale 1:46 compared to uh SQL or or relational 1:49 databases. They're also much faster in 1:52 most types of operations. There are some 1:55 things that uh would you'd benefit by 1:59 using relational databases, but when 2:01 you're dealing with a just a crapload of 2:04 data, uh then NoSQL is usually the way 2:07 to go. All right, as long as there's not 2:10 really uh a ton of interconnected 2:13 relationships. All right, so enough 2:16 talking 2:17 about methodology. Let's jump in and get 2:21 started. So I'm using Windows in the 2:23 course I mentioned we do use Linux 2:25 Abuntu quite a bit, but for this 2:27 particular video we're using Windows. 2:29 So, we're going to go to 2:31 mongodb.com. And you want to click the 2:33 download button here. And that should 2:36 take us to the download page. And we're 2:40 going to go ahead and choose this 2:42 version here. Uh, it says Windows Server 2:45 2008. That's fine. 2:48 64bit. So, we're going to go ahead and 2:50 download that. All right. Which I 2:52 already did. So, it's 104 megabytes. So, 2:54 it might take a couple minutes, but I 2:57 already have it downloaded. So, I'm 2:58 going to cancel that and we're going to 3:00 go ahead and run that MSI file. So, 3:03 we're going to click next and just go 3:05 through this. We'll accept the terms. 3:07 Now, right here, I'm going to choose 3:08 custom because I want to change the 3:10 location. Okay. By default, it's going 3:12 to go in your program files folder and 3:15 then MongoDB server 3.2. I want it much 3:18 more simple than that, the path. So, I 3:21 want to just go right to my C drive. 3:25 So in my C drive, I'm going to create a 3:27 folder called 3:29 MongoDB. And then we're going to just 3:32 open that and click okay. Okay. So much 3:35 simpler. It's just right in the C drive. 3:37 Okay. We'll click next and then 3:40 install and we'll say 3:44 yes. Okay. So that's finished. We'll 3:47 click finish. And there's still a couple 3:49 steps we need to do. We need to open up 3:51 a command line. 3:54 as 3:57 administrator. Okay. And I have that 3:59 nice and big so you can see it. And also 4:02 we're going to go to that folder in C 4:05 drive MongoDB. And we're going to create 4:07 a couple folders here. We're going to 4:09 create one called 4:10 data. And we're going to create one 4:13 called log. All right. And then inside 4:16 data, we're going to have another one 4:18 called DB. Okay. And that's where all 4:20 the data will be stored on the file 4:22 system. And now let's go to our uh 4:25 command line here and we're going to 4:26 navigate to the MongoDB 4:30 folder and then into into the bin 4:35 folder. All 4:37 right. Now in here we need to run MongoD 4:41 and we're going to add a bunch of flags 4:43 here. So the first one is going to be 4:46 directory per 4:49 DB. All right. And then we want to 4:52 specify DB path which is going to be the 4:55 the path to the the folder we just 4:57 created. So C drive slash 5:02 MongoDB 5:04 slashdata slash DB. And if you didn't 5:07 install in in your C drive MongoDB just 5:10 put whatever location you used. All 5:12 right. And that's why I wanted to change 5:14 that that uh that long location path. 5:18 All right. So after that we're going to 5:20 say d-log 5:23 path and that's going to be C 5:26 drive 5:28 MongoDB 5:29 slash log and then we're going to say 5:33 slashmongo.log. Okay, so that's going to 5:35 be the file that all the MongoDB logs 5:38 write to. And then we just want to 5:40 specify 5:41 d-log 5:43 append and 5:45 d-rest and then 5:47 d-install. Okay. And that's going to 5:49 allow us to run it as a service. So 5:52 let's go ahead and run that. And now we 5:55 should be able to run the service with 5:59 net 6:02 start 6:03 MongoDB. Okay. Okay. And now it says 6:05 it's starting and it started 6:08 successfully. Okay. So we now have 6:09 MongoDB installed and it's also running 6:12 in the background as a 6:14 service. Now we're going to be working 6:16 in the shell and we can run that 6:18 by simply typing from within the 6:20 bin directory. All right. So we're now 6:23 in the shell. I'm just going to do I 6:25 think cls and that clears everything 6:28 out. Now to show us a list of databases, 6:31 we can do show DBs. And it shows us that 6:34 there's one default database there 6:37 called local, which we're not going to 6:38 touch. We want to create a new database. 6:41 So to do that, we're going to say use. 6:43 And then whatever we want to call it, 6:45 we'll say my 6:47 customers. And you can see that not only 6:49 did it create it, but it also switched 6:51 us to that database. And if you ever 6:53 want to check what current database 6:55 you're in, you can just do DB and it'll 6:58 tell you. Now, I just want to quickly go 7:00 over the syntax for a document. So, I'm 7:04 going to open up Notepad. And a lot of 7:06 the queries that I do, I'm going to put 7:07 Notepad first and then just copy them 7:10 and paste them in just for readability. 7:13 All right. Now, basically, when we're 7:14 working with a document, it's just like 7:16 a JSON object or a JavaScript object. we 7:19 have a set of curly braces and then we 7:21 have let's say a field with a value okay 7:26 and then a comma and you can just keep 7:27 adding fields. So let's say if we have a 7:31 customer we may want them to have a 7:33 first 7:35 name. Okay, strings will have quotes 7:38 around them and let's say last 7:42 name. All right, so these are just very 7:45 simple string fields. All right, are 7:48 string values. Um, if you want, we can 7:51 also have an array as a value. So, let's 7:56 say maybe there's a 7:59 memberships and we want an array with 8:02 all the different memberships that this 8:05 user is in that this customer is in. So 8:07 maybe me one, me two and so on. Okay, so 8:10 we can have arrays. We can also have 8:14 objects. So let's say address and that's 8:18 represented with curly braces and you 8:22 might have a 8:28 street and a 8:31 city and so on. Okay, you can also have 8:35 uh arrays of objects. So 8:38 maybe we'll have one called 8:40 phones and 8:43 uh actually is that that's not really a 8:45 good 8:46 example. Maybe 8:49 contacts. So if we have an 8:54 array and you may have let's say 8:58 name Brad. Whoops. 9:05 and then 9:07 maybe 9:10 relationship friend and so on. All 9:12 right. And you could do a comma here and 9:14 then add another one. So you can have 9:16 different types of 9:18 values. Now before we go and we start 9:20 inserting data, I want to just create a 9:23 simple user for this database. So if we 9:25 go to I'm just going to search for it. 9:28 We'll say 9:30 MongoDB. Create user. 9:36 Okay. So, 9:38 db.create user will create a user for 9:41 that database in this format here. So, 9:44 we're going to say over here db 9:47 dot create 9:50 user and we'll pass in some curly 9:53 braces. And let's specify what is it? 9:58 User. User. And we'll just say Brad and 10:03 then password or 10:07 pwd and 10:10 rolls. Okay, rolls I believe is an 10:14 array. Okay, it can be an array of 10:16 objects, but I think we can make it more 10:19 simple. Yeah, I just want this right 10:22 here, read, write, so that we can read 10:24 and write to the database and also as an 10:26 admin. So, I'm just going to grab 10:29 that and we'll paste that in. And then 10:32 let's copy that. Paste that in and run 10:36 it. And now you'll see it says 10:37 successfully added user. Okay. So, now 10:41 that we have a user, let's start to add 10:44 some data. So, to do that, we use 10:47 insert. So, we say db dot and then 10:50 whatever the the Oh, we didn't create a 10:52 collection, did we? All right. So, let's 10:54 back up a little bit. Collections are 10:57 very similar to tables in a relational 10:59 database. Basically, they're just they 11:02 hold documents or records. So to create 11:04 a collection, we're going to say db 11:07 dot create 11:09 collection and pass in the name of the 11:11 collection, which will say 11:14 customers. All right. And then if we 11:16 want to see all the collections in this 11:18 database, we can simply say show 11:20 collections. All right. Now, if we want 11:22 to insert a document into that 11:24 collection, we say 11:26 dbc 11:30 customers.insert. All right. And then 11:32 that's going to take in uh some curly 11:34 braces and let's say first 11:39 name 11:44 John, last 11:46 name do. Okay. So if we go ahead and run 11:50 that we get an inserted one. Okay. So it 11:54 inserted one 11:55 document. Now if we want to see the 11:58 documents in a collection we can say 12:02 db.c 12:06 customers.find. And you can see it gives 12:08 us that uh list of documents which we 12:11 only have one has our first name and 12:13 last name but it also has this uh 12:15 underscore ID field which is set to be 12:18 an object ID. All right. And this is 12:20 used as a unique value uh to find 12:24 documents and and do some other stuff. 12:26 But notice that it was automatically 12:27 created. Okay. So we don't have to worry 12:30 about creating an ID field setting it to 12:32 auto increment set setting it as a 12:35 primary key. These are all things that 12:36 you have to do in a relational database. 12:39 All right. So, let's go ahead and add a 12:42 couple 12:43 more. Now, if we want to add multiple 12:47 uh documents at once, we can just make 12:49 an array. So, we'll put square brackets, 12:53 oops, square brackets around it like 12:55 that. And let's change this 12:59 name. We'll change him 13:03 to Stephen Smith. 13:06 And then let's put a comma here. And 13:08 then another user or customer with a 13:12 first 13:14 name. We'll say Joan 13:18 uh last 13:21 name we'll say 13:24 uh Johnson. Okay. 13:27 Now, with a relational database, we 13:30 would have to specify that the 13:33 customer's table would have to have a 13:35 first name and a last name, and that's 13:37 it. They couldn't just on the fly add 13:40 another field. And that's what's great 13:42 about NoSQL is we can do that. So, for 13:45 this Joan Johnson, let's say we want to 13:48 add a 13:50 gender, and we'll set that as female. 13:54 Oops. 13:56 Okay. So, we'll set that as female. And 13:58 then let's go ahead and 14:00 enter. And now if we do a 14:03 find, you'll see that Joan has a gender 14:07 field. Okay? Even though we didn't 14:09 specify that for John and Steven, it's 14:11 fine. Okay? You can have whatever fields 14:14 you want on whatever um documents you 14:16 want. All right. Now, we only have a 14:19 couple fields. So this is this looks 14:21 nice and clean, but if you have a ton of 14:23 fields here, um it can get kind of 14:26 messy. So we can add on to find we can 14:29 say dot pretty just kind of a helper 14:32 function. And you'll see that it makes 14:34 it nice and neat for 14:36 us. All right, so let's look at updating 14:39 a field. All right, so let's say that 14:41 for John Doe, we want to add a gender to 14:43 him as well. So what we would do is 14:46 db.cs 14:52 customers.date and first parameter is 14:55 going to be a match. So we want to match 14:59 name or actually let's do first 15:05 name to John. Okay. So it's going to 15:08 match anyone that has the first name of 15:10 John. Now, if you're building a 15:12 production application, you probably 15:14 don't want to use something like first 15:15 name because other people can also have 15:17 that name. So, it's going to update all 15:20 of them. You'd probably want to use 15:21 either the object ID or some other 15:23 unique field. But just for simplicity, 15:26 we're going to use first name. So, then 15:28 the next parameter is going to be what 15:30 we want to replace it with. Okay. So 15:33 we'll say first 15:35 name 15:36 [Music] 15:38 John last 15:41 name 15:42 do and 15:45 gender male. All right. So we'll go 15:48 ahead and run that. It says it matched 15:50 one modified one. So if we look at the 15:53 find now you can see that John Doe has a 15:56 gender of male. Now notice when we did 15:59 the update, we had to also specify first 16:03 and last name again, even though it was 16:04 already there. If we didn't do that and 16:07 we just said gender male, it would it 16:09 would basically replace the entire thing 16:12 with just that gender male. Now, there 16:14 is a way around that and that's by using 16:16 the set operator. All right, so let's 16:19 say 16:20 uh Steven Smith, we want to add a gender 16:23 to him. All right. So, we can say 16:26 update and let's say first 16:29 name equal to 16:33 Stephen. And then for the second 16:35 parameter, I'm just going to get rid of 16:38 this. Okay, we do want to keep the curly 16:40 braces. And inside there, we're going to 16:42 say dollar sign set. All right, and then 16:45 we're going to set that to another set 16:46 of curly braces. And we'll say 16:49 gender male. So using set, it's going to 16:53 keep whatever is there previously and 16:55 then just add this on. So if we go ahead 16:58 and run that and then we do a find, 17:00 you'll see that Steven Smith still has 17:02 his first name and last name and just 17:05 added the 17:07 gender. Now we also have uh an operator 17:10 called ink which can increment uh 17:12 numeric values for us. So first thing I 17:15 want to do is just add to 17:17 Steven. I'm going to 17:19 add an 17:22 age and let's say 45. Actually, we want 17:26 to get rid of the 17:27 quotes. 17:29 45. All right. So, now if we do a find, 17:32 I can see that he has an age of 45. Now, 17:34 if we want to increment that, we can say 17:37 db 17:40 dot customers.update 17:44 update and we want to 17:47 match first 17:50 name Stephen and then here we want to 17:54 put our curly braces and then we're 17:56 going to specify dollar sign 18:01 inc. And then we want to age and then 18:04 the number we want to increment. So 18:06 let's increment five. Okay, he just aged 18:09 five years. 18:12 Uh, let's see what Oh, I didn't put 18:15 curly braces around 18:22 this. All right. So now if we do find, 18:26 you can see that Steven is now 50. Okay, 18:28 it added five 18:30 incremented. Now we also have an 18:32 operator called unset if we want to 18:35 remove a field. So let's say DB dot 18:42 customers.update 18:45 update and we want to 18:48 specify 18:49 first 18:51 name 18:54 Stephen and then let's 18:57 put for our next parameter we're going 19:00 to say uh dollar sign 19:03 unset 19:05 and we'll say unset I think we can do 19:09 unset age let's try that nope is not 19:13 defined find. All right. So, we need to 19:15 do 19:16 unset and then curly 19:19 braces and then 19:22 age. I think it's like that. Yeah. Okay. 19:25 So, age one. And then let's do a find. 19:28 And now Stephen doesn't have age 19:31 anymore. Now, let's see what happens if 19:33 we try to update something that isn't a 19:35 match, something that isn't there. All 19:37 right. So, let's say 19:40 um db.c customers 19:44 update and put in some curly braces and 19:47 we'll say first 19:52 name first name Mary and then we want to 19:57 update we'll say first 20:02 name 20:04 Mary and last 20:09 name Samson. 20:11 All right. So, let's go ahead and run 20:13 that. And you'll see that the result is 20:16 zero all across. So, if we do a find 20:19 now, it's just it's the same. But if we 20:22 want to to say if this isn't found, then 20:25 insert it. We can add a uh an option 20:29 called upsert. So, let's go back to that 20:32 query. And then we're going to set a 20:34 third parameter here, which is some 20:37 options. And we're going to say 20:39 upsert set that to true. And then we'll 20:43 run that. And now you can see that we 20:45 get one inserted or upserted rather. And 20:48 it also includes an object ID. So now if 20:50 we do our find, you can see that Mary 20:54 has now been added and an object ID has 20:57 been 20:58 created. Now we also have an operator 21:01 called rename that we can use with 21:02 update. So if we say db.c 21:07 customers do.date 21:11 update DB customers update and let's 21:16 say where first 21:18 name is equal 21:23 to 21:26 Stephen and then for the second 21:28 parameter here we're going to say 21:31 um let's do curly braces and then 21:36 rename and we should be able to rename. 21:39 Let's say gender. We'll rename that to 21:48 sex. I think that's the 21:51 syntax. Rename. Oh, no. Wait. We got to 21:54 put curly braces around 21:59 this. 22:01 Okay. All right. So now if we do a find 22:04 now you can see that with Steve Smith 22:06 gender has changed to 22:08 sex. All right. So those are some of the 22:11 operators we can use with update. Now to 22:13 remove documents is very easy. We can 22:16 just say db.c customers.reove 22:22 remove and then let's specify where 22:26 first 22:28 name is equal to 22:31 Stephen. Okay, you can see we get n 22:33 removed one and now if we do a find 22:36 Steven's gone. 22:38 Now, by default, that query we just ran, 22:42 the remove, it would delete all 22:45 customers with the first name of Steven, 22:47 but you could add a parameter 22:50 uh here, you could set inside your 22:52 options. Um, there's one called just 22:56 one, and you could set that to true. All 22:59 right, I believe it's true. True or one, 23:02 but you can set that and then it's only 23:04 going to delete the first Steven it 23:05 finds. It's not going to delete all of 23:07 them. All right. So, just kind of a a 23:09 safety option. All right. So, I want to 23:12 show you a few other things, but we I 23:14 want some more data to work with. So, 23:16 I'm going to in Notepad, I'm going to 23:19 just paste in a query to add some more 23:24 data. All right. So, we basically just 23:27 have an insert here with some fields for 23:29 our customers. first name, last name, 23:31 gender, age, address, which is an 23:34 object, uh memberships, which is an 23:37 array, and then balance. Okay, so we 23:39 have what is it? I think five, four or 23:42 five of them. So, I'm going to copy 23:46 this and we're going to go over here and 23:48 paste that in and run it. You can see 23:51 it's inserted five. So if we go and we 23:54 do 23:55 find, you can see the different 23:58 customers that were added. All 24:01 right. Now I think up to this point 24:03 we've just done find with no parameters. 24:06 So what I want to do is 24:08 DB 24:11 customers.find and we can pass in a 24:14 query here. So let's say we want to find 24:18 Sharon. So, we could 24:20 say first 24:24 name Sharon. Okay. And that's going to 24:27 give us all Sharon's 24:29 information. Now, let's say we wanted to 24:32 find Sharon and Troy. All right. So, 24:35 what we could do is use the we could use 24:37 the or operator. So, let's say 24:41 DB.Customers.find 24:46 find and let's pass in our curly braces 24:50 and then we're going to say dollar sign 24:52 or set that and then put some square 24:55 brackets for an array and we'll say 24:59 first 25:02 name 25:04 Sharon. Put a comma here and we'll say 25:08 first 25:11 name Troy. Okay, if we run that, you can 25:14 see that it gives us back Troy's 25:16 information and Sharon's. Okay, now we 25:19 can do a simple find 25:22 for uh a different field that is going 25:25 to match multiple results. So, for 25:27 instance, gender. If we say find 25:32 gender and change that to male, if we 25:36 run that, it's going to give us 25:37 everybody that has the gender of male. 25:42 Now we can also use greater than or less 25:44 than operators. So let's say we want to 25:47 find everyone that is under the age of 25:50 40. So we'll say db 25:54 dot 25:57 customers do 25:59 find and in here we're going to pass 26:04 in age. 26:09 And in here, we're going to set we're 26:11 going to open up some curly braces and 26:13 we'll say dollar sign gt and we'll set 26:17 that to actually do we want to do less 26:19 than we'll do 26:21 lt. Set that to 26:24 40. Okay. And that's given us back 26:27 actually let's add pretty to the end 26:31 here. Okay. So that gives us back 26:33 everybody that is under 40. You'll see 26:35 Troy's 33, 23, and 35. All right. And 26:40 obviously, you can do GT for greater 26:42 than. All right. Now, there's also LTE, 26:45 which is less than or equal to, and GTE, 26:48 which is greater than or equal 26:50 to. Now, let's say we want to find 26:54 everyone that lives in the city of 26:56 Boston. So, you can see that we have for 26:59 our address, which is an object, which 27:02 has street, city, and state. So what we 27:04 can do is we'll say 27:08 db.c 27:11 customers.find and 27:14 here we're going to say we're going to 27:17 use quotes here and say 27:20 address dot 27:23 city and then here we'll say Boston. 27:28 Okay, we run that and it gives us the 27:30 two people that have their city as 27:31 Boston. 27:34 Okay, when we're dealing with uh objects 27:36 like this, we have to wrap it in quotes. 27:38 You'll see if I go and take these quotes 27:43 off, it gives us an 27:45 error. And I should mention that there 27:47 are some clients and programs that will 27:50 require you to use uh quotes in the key 27:54 as well. So, just remember that if 27:56 you're having issues in some other 27:57 client, just try that and see if that 28:00 helps. 28:02 All right. Now, if we want to query the 28:04 memberships, okay, remember memberships 28:06 is an 28:07 array, then that's pretty easy. All we 28:10 have to do is 28:13 customers.find and let's say 28:16 memberships and then just the name of 28:18 it. So, let's say me one. Okay, so 28:21 that's going to give us everyone that 28:22 has mem one inside their memberships 28:26 array. All right. Now, next thing I want 28:29 to talk about is sorting. Okay. So, 28:31 let's do a find. We'll say actually I'm 28:33 going to clear this 28:35 out. And we'll do 28:38 dbc 28:42 customers.find. And then what we can do 28:44 is we can add on to this. We'll say 28:47 dotsort. Okay. And then that's going to 28:49 take in whatever field we want to sort 28:52 by. So, let's say 28:57 um we'll do last name. So we could 29:02 say last name one. Okay. So if I set it 29:08 to one, that means that it's going to be 29:09 in ascending order. So if we look at the 29:12 last name, we have do Jenkins Johnson 29:15 Samson Wilkins. Okay. If we wanted 29:17 descending, then we could just set it to 29:19 negative one. And then you can see 29:23 uh Whoops. I want to 29:25 do 29:29 pretty. Okay. Now you can see it's going 29:32 in the opposite 29:33 direction. Okay. We have Wilkins at the 29:36 beginning. All right. So that's 29:39 sorting. We can also count documents. So 29:42 if we want to do db.c 29:46 customers.find.count 29:50 count and run that. You can see that it 29:53 gives us eight. Okay, we can also put a 29:55 query in here if we wanted to. If we 29:58 say 30:01 gender male and it gives us four. Okay, 30:04 because there's four people that are 30:06 males. We can also limit. So if we want 30:10 to 30:12 find we'll find everybody but we're 30:15 going to set a 30:20 limit. We'll set limit to four. And you 30:24 can see that it gives us the first 30:27 four. And of course you can combine 30:29 these. So we could say limit 4 dot 30:35 sort and we'll sort 30:39 by let's see last 30:42 name one and there we 30:47 go. Another cool thing we can do is we 30:50 can iterate through stuff using for 30:52 each. All right so we'll do 30:56 db.c customers.find 31:00 And then we want to say 31:02 dot for 31:04 each. And then in here we have a 31:09 function. We want to pass it a variable 31:13 which can be anything. I'm just going to 31:14 say doc. And then let's say we want to 31:17 print. Okay, this is a print function we 31:19 can use. And we'll print the 31:22 string 31:25 customer name. And then we can 31:28 concatenate onto that with a plus sign. 31:30 Okay, just like regular JavaScript. And 31:32 we'll say doc 31:35 dot and then whatever field first 31:38 name. And now we get we're printing out 31:41 customer name and then whatever the 31:43 first name is. All right, so we're going 31:46 to go ahead and stop here. Now, if you 31:49 enjoyed this, I would suggest checking 31:51 out the 10 project MongoDB course. Um, 31:54 there are a couple projects like this 31:56 where we're just dealing with the 31:57 database and the shell, but there's also 31:59 projects where we're building web 32:01 applications, implementing uh different 32:04 technologies like Angular and NodeJS. 32:07 So, I would suggest that.