Start a free Courses trial
to watch this video
In this video we look at how to insert new records into our database, and how to perform simple queries
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up[?mellow guitar music?] 0:00 Think Vitamin Membership - Est. 2010 membership.thinkvitamin.com 0:03 MongoDB - Basic Queries with Jim Hoskins 0:07 In the previous video, we got MongoDB installed and running on your system. 0:13 Now we're going to take a look at how to insert data 0:17 and perform some basic queries in MongoDB. 0:19 So, I have my Mongo demon server up and running, 0:22 like we set up in the first video, 0:24 and in another command window, I'm going to start up our Mongo process 0:26 for interacting with our database. 0:30 So I'm just going to type in ./mongo. 0:32 we get the MongoDB shell. 0:34 Now, the first thing you're going to notice is we are connecting to test. 0:36 This means that the database that we're using is called "test." 0:39 We can actually change to a different database by using the use command, 0:42 so we can do >use foo 0:46 and now we're switching to a different DB. 0:48 I'm actually going to be playing around in the test database, 0:50 so I'm just going to say >use test. 0:52 So I'm just going to clear this out. 0:55 Now the first thing you'll notice is that the MongoDB terminal 0:57 is just an interactive JavaScript environment. 0:59 So, you can do things like evaluate simple JavaScript, like 2 +2 1:01 or set variables, like x = 7 1:05 and do things like x * x and get values back. 1:09 And strings and stuff work, too, so we can do "Hello" + " World" 1:14 and we can do simple JavaScript operations like that. 1:20 Now, that's not a full browser JavaScript, so things like the dom and other attributes 1:23 are not going to be in here, but the core language of JavaScript is available 1:27 in the MongoDB terminal. 1:30 So I'm going to clear this out again. 1:32 Let's look at creating a document for us to store in our database. 1:34 A document is simply a JavaScript object which has keys and values. 1:39 So, for instance, I could create the variable jim 1:43 and start with an open curly brace to create our object 1:46 and we can span it across multiple lines, 1:49 so I'm going to just set some values, like first_name: "Jim" 1:51 and I'm going to set it to a string value here, 1:55 and then trail with a comma, 1:57 do last_name: "Hoskins", 1:59 and then we''ll do like, height: 79, 2:03 and we can do skills: 2:09 and we can actually do things like array, so ["JavaScript", "Ruby", "Python"] 2:13 and I think that's enough for my objects. 2:25 I'm just going to close it up with a closing curly braces. 2:27 So we can see we got a representation back of the Jim object, 2:30 which is just first_name, last_name, height, and skills. 2:34 You'll notice I didn't use quotation marks on the keys here, 2:38 because I didn't have to, because in the JavaScript syntax, 2:41 if you're just using simple variable names, 2:44 you can omit the quotation marks on the key values. 2:46 Of course, if you have a key value with a space or any other special character, 2:48 you're going to want to put them in quotation marks so you can have multiple word keys 2:51 or any other special characters in them. 2:56 Now we actually want to go ahead and save this into the database. 2:59 We can see our object jim still exists. 3:02 In MongoDB, we use collections as a way to organize our data. 3:05 They're sort of like tables--you can think of them 3:10 as just different bins for our records. 3:12 Now, they don't enforce any schema; 3:14 they are just a way of separating the types in your database. 3:16 For instance, you may store all your books in one collection 3:19 and all your people in another. 3:22 Now, you could store your books and your people in the same collection, 3:24 but it's probably more convenient to keep them separate. 3:27 So I want to store jim into the people collection, 3:30 and to do that, I just get our database object, which is the db variable 3:33 in our environment here, 3:37 and I just give it the name of my collection, so I'm just going to call .people 3:39 and then on that, I'm going to call the .save method 3:43 and pass it the record I want to save--in this case, (jim). 3:46 So when I do that, we have now saved our jim record into the people collection. 3:50 Now, you'll notice I didn't have to create the people collection-- 3:55 it didn't actually exist before I used it. 3:57 So it will automatically create it. 3:59 Now, if you really need to, you can predefine collection names 4:01 and even predefine the maximum size of collections, 4:04 but for us, it's just easier to define our collections as we go. 4:07 I'm just going to clear this out. 4:11 And now, if I want to get my record back, I can just call db.people 4:13 and on the collection, I can call the .find() method. 4:17 I'm not going to pass it anything right now, 4:20 because I just want to find all the records in it. 4:22 So you'll see that when we do that, we get 1 record back, 4:24 and it looks just like our record-- 4:27 we have a first_name, last_name, height, and a skills array-- 4:29 but we also have the special "_id" property here, 4:33 and this is the internal database ID that represents our object, 4:36 so it should be a unique ID inside of our database. 4:41 It's going to be automatically assigned to any record we put in, 4:45 unless we already have it assigned, 4:48 but for the most part, we're not going to worry about setting our object ID-- 4:50 it's just used for reference, and we'll see that later. 4:53 And so, if I wanted to insert another person, I could just do db.people.save 4:56 and I'm going to just pass it an object right here, 5:02 so I'm just going to say ({name: "Sally"}). 5:05 And I can do that, and if I do db.people.find() again, 5:09 you can see now we have 2 objects: 5:13 my Jim object and my Sally. 5:15 Now, you'll notice they don't have to even share any of the same attributes. 5:18 Here, I did "first_name": Jim, "last_name": Hoskins, 5:21 but here I just did "name." 5:24 So that's one of the cool things about MongoDB, is there's no schema to it, 5:27 so you can just store the information you need. 5:30 Now obviously in a lot of applications, 5:32 most of your objects are going to have similar attributes, 5:33 but you don't need to have it, so if you ever want to add more attributes 5:36 to some objects, it's really easy to do. 5:39 Now, we can use normal JavaScript to automate inserting records. 5:42 For instance, in this case, I'm going to create a collection of documents 5:45 in the collection called squares, so I'm going to just loop through the numbers 1 through 99 5:49 and I'm going to call db.squares.save 5:53 and I'm going to give each document a property "n", which will be "i," 1 through 99, 5:58 and the property square, which will be "n" squared, or "i" squared. 6:03 So I can see I have multiple lines here, 6:06 and I'm just going to commit this 6:09 and so now we can do something like db.squares.find() 6:12 and you can see we obviously have a lot of records. 6:18 You can see we have so many that it actually says "has more" down here, 6:21 so if you ever get that, you can actually iterate over your collection of results 6:25 by just typing "it" for iterate 6:29 and you can see we get more, and more, and more, until we're done. 6:31 Now if I wanted to find a specific record in our squares collection, 6:35 I could do db.squares.find() and the first argument I'm going to pass to it 6:39 is an object, and I'm going to specify that I want any of my records to have "n" 6:43 have the value 12. 6:49 So, in this case, when I call it, you can see there's obviously only 1 record 6:52 with the value "n" as 12, and its "square" is 144. 6:56 So this simple syntax of passing an object with the key and value you want 7:00 is a simple way of doing a search for a specific value in the database. 7:04 Now, if I wanted to search for all the records that are less than 12, 7:08 I could do db.squares.find() and I'm going to pass an object in again. 7:12 I'm going to give it the property "n," but instead of giving it a single value, 7:18 since I want to have a comparative operator like "less than," 7:23 instead of just passing the value 12, I'm going to pass it another object. 7:26 Inside this object, these are going to be the conditions that "n" has to meet 7:30 in order for it to return in this set. 7:35 So there are special keys that we can use, and one of them is the $ less than-- 7:37 or $lt-- 7:42 and when we pass it that value, we're saying we want it to be less than 12. 7:44 So when you see $ here, these are special operators 7:48 that have special meaning in MongoDB. 7:51 So in this case, I'm saying db.squares.find where "n" is less than 12. 7:54 Now, when I go ahead and hit enter, 8:00 you can see that we get 1, 2, 3, 4, 5, 6 all the way up through 11. 8:02 Now we saw that we got both the "n" and the squares property in our query before. 8:06 Let's say I only want to get the squares property. 8:11 In the find command, I can pass a second argument, 8:14 which is an object of the keys that I want to return. 8:17 Now, if I don't pass anything, it's going to return all the keys, 8:20 but if I pass an object with something like square: and just the key 1 to say true, 8:22 you can see that we now only get the square property back. 8:29 In the next video, we're going to take a look at some more advanced techniques 8:33 for querying our database. 8:37 [?mellow guitar music?] 8:39 Think Vitamin Membership - Est. 2010 membership.thinkvitamin.com 8:41
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up