In this lesson, we will pull our data from MongoDB to Mongoose.
A sandbox is an environment you can play around and learn in. 0:00 And it's a great way to get acquainted with new technologies. 0:04 Let's create a sandbox to get a feel of how Mongoose works. 0:08 To get started using Mongoose, we'll need to install it. 0:12 I'm in the terminal, in my project folder. 0:17 And I'm going to install Mongoose with NPM using the save flag 0:21 When that's done hop over to your text editor and 0:40 let's create a temporary file for some experiments. 0:44 What we call it isn't important. 0:48 I'm going to call mine mongoose_sandbox.js 0:51 At the top of the file let's use strict. 1:03 And require the mongoose module. 1:12 Then we want to connect to the Mongo DB server. 1:25 We do that by calling the mongoose.connect method. 1:29 This takes one parameter, a connection string, 1:39 in our case it's mongodb:// which is the protocol we're using. 1:45 This is because we're talking to a mongodb database. 1:54 Then its local host, Which 1:58 is our local machine, :27017, 2:03 which is the default port for mongodb. 2:08 Then, /sandbox, which is the database name to sandbox. 2:13 We could name this anything we want, but sandbox is a sensible name. 2:20 Once this call is made, 2:25 we can monitor the status of the request through Mongoose's connection object. 2:27 Let's reference the connection object and store it in a variable called db. 2:38 The db object will now emit various events related 2:47 to the database connection that we can listen for. 2:51 When events occur, we can handle them. 2:55 One event that could occur is an error. 3:03 We hope an error doesn't happen, but 3:07 if it does we want to have some code to handle it. 3:09 We can listen for the error event with the on method. 3:13 For our error handler, we can log a message to the console so 3:26 we know if there were problems. 3:31 Now let's listen for the open event. 3:50 This event is emitted when the connection is ready to go. 3:53 It means that the connection to the mongodb server is open and 3:57 it's ready to talk. 4:00 We'll use the once method to listen for it. 4:02 This method is like the on method, 4:11 except while on fires its handler every time an event occurs, 4:13 once will only fire its handler the first time the event occurs. 4:18 This can relieve the app from listening when it no longer needs to. 4:23 Let's create a handler. 4:28 And blog out that the database connection was successful. 4:33 This handler is where we'll write all of the codes to communicate with 4:49 the database. 4:53 When we're done communicating, we can close the database connection. 5:06 First, we'll create a schema. 5:22 To do that, we'll reference the schema constructor from the mongoose object. 5:25 Let's make a schema to track animals called AnimalSchema. 5:36 We'll do this by calling new on the schema construct a function and 5:47 pass in an object. 5:51 The object we're passing into the schema constructor will describe our data. 5:59 Let's say, we wanted to track the type of the animal as a string. 6:09 The color of the animal as a string. 6:18 The size of the animal as a string. 6:25 The average mass of the animal in kilograms. 6:33 Which is a number. 6:40 And then the name of the animal as a string. 6:44 We'll use the animal schema to create a mongoose object called a model, 6:56 which will create and save our document objects. 7:01 Let's create a model called Animal. 7:04 We do this by using the mongoose method model. 7:11 Passing in the name of the model, Animal, and the schema that defines animals. 7:19 Mongoose will pluralize the string we supply as the first parameter here. 7:31 This will map to a collection in the mongodb database 7:37 whenever we save the document. 7:41 In this case the model will create or map to a collection named animals. 7:44 Let's create an animal document now. 7:49 Let's create an elephant calling the animal constructor function. 7:56 In the constructor function, it takes an object with all the properties and 8:15 values your model needs. 8:19 In this case, we're filling in all the attributes for 8:21 an elephant called Lawrence. 8:24 When we run this code an elephant in memory of our application is created. 9:00 However, Mongo will not know about it until we save it. 9:07 In fact, if we run this right now and 9:11 went into Mongo to look for an animal's collection we won't see it. 9:14 it's not been created by Mongoose yet. 9:18 When we save this elephant, Mongoose will create the animals collection and 9:20 save the elephant object in the new animals collection, all in one action. 9:24 Let's save it by calling the save method on the elephant object. 9:33 Now we have a problem. 9:42 This code won't work. 9:44 That's because save is an asynchronous method. 9:45 Our application will call the safe method first and 9:49 then call close right away before safe can finish, causing it to fail. 9:53 What we'll need to do is pass in a callback function into the save method, 9:58 and then close the database connection from inside the callback. 10:02 The save callback passes in an error as its parameter. 10:23 Let's handle that by logging out save failed. 10:29 If it does save, let's logout saved. 10:50 When the save is done the callback is fired and then the database will close. 11:02 We can pass in a call back to the close method to be notified 11:08 when the connection is closed. 11:12 Now 11:13 we're 11:17 going to test this out. 11:25 Make sure you've got your Mongo DB7 running. 11:34 Switch over to your terminal and then run the sandbox file. 11:37 Let's peek inside Mongo to see what we did. 11:43 First, use the database name from the connection string we used earlier. 11:49 In this case for me, it's sandbox. 11:55 Next we can get the collection names in our database. 12:07 There's our animals collection. 12:11 Let's find what animals are in our collection. 12:14 As you can see we have a collection named animals that contains one document and 12:19 then with a name to Lawrence. 12:24
You need to sign up for Treehouse in order to download course files.Sign up