1 00:00:00,580 --> 00:00:05,420 Let's say we wanted to create the default animal to be saved if we don't supply 2 00:00:05,420 --> 00:00:06,710 any data to the model. 3 00:00:08,370 --> 00:00:11,910 Mongooses schema lets us as defined default values very easily. 4 00:00:13,350 --> 00:00:17,688 Let's change the schema to give us a goldfish animal as default. 5 00:00:24,004 --> 00:00:26,801 Instead of just specifying a type, 6 00:00:26,801 --> 00:00:30,914 we can give each property a type and a default value. 7 00:00:48,284 --> 00:00:54,489 Goldfish are small, 8 00:00:54,489 --> 00:00:59,285 golden and really light in 9 00:00:59,285 --> 00:01:04,301 comparison to elephants, 10 00:01:04,301 --> 00:01:07,797 and named Angela? 11 00:01:12,559 --> 00:01:17,296 Anyway, now we can create a generic animal with our model by 12 00:01:17,296 --> 00:01:19,962 passing an empty object into it. 13 00:01:33,124 --> 00:01:36,807 And when I say a generic animal, I mean a goldfish. 14 00:01:44,117 --> 00:01:48,235 If we were just to call save on the animal, 15 00:01:48,235 --> 00:01:53,540 under elephant.save, we could run into problems. 16 00:01:56,640 --> 00:01:59,990 The save method runs asynchronously. 17 00:01:59,990 --> 00:02:03,570 It's impossible to know which save will finish first. 18 00:02:04,690 --> 00:02:09,110 If the elephant saves finish first, it will close the database connection. 19 00:02:11,420 --> 00:02:14,920 The generic animal won't be saved. 20 00:02:16,220 --> 00:02:22,343 Instead, we'll place the goldfish.save inside the other's callback and 21 00:02:22,343 --> 00:02:26,393 put the db.close inside the goldfish's callback 22 00:02:26,393 --> 00:02:29,800 to ensure the correct sequence of events. 23 00:02:59,441 --> 00:03:03,495 Remember we already have an elephant in the database. 24 00:03:03,495 --> 00:03:05,795 Running this file again will create another one. 25 00:03:06,930 --> 00:03:09,870 Let's empty the animals collection before we save it. 26 00:03:11,020 --> 00:03:13,770 The model can do this for us. 27 00:03:13,770 --> 00:03:18,249 Let's ask the model to empty our animals collection before we save anything. 28 00:03:25,646 --> 00:03:29,928 We can do this by calling the remove method on the animal model and 29 00:03:29,928 --> 00:03:31,844 passing in an empty object. 30 00:03:40,267 --> 00:03:45,065 We could specify which documents we want to remove by passing in a query 31 00:03:45,065 --> 00:03:47,000 object here. 32 00:03:47,000 --> 00:03:51,369 But if we leave all of the properties blank, it will remove all documents. 33 00:03:52,700 --> 00:03:55,980 A query object would match properties from itself 34 00:03:55,980 --> 00:03:58,650 against properties of documents in the database. 35 00:03:59,960 --> 00:04:02,449 Any matching documents would be removed. 36 00:04:11,454 --> 00:04:15,857 We want all the save actions to take place after the removal, so 37 00:04:15,857 --> 00:04:19,029 we'll route this code in removes callback. 38 00:04:52,236 --> 00:04:59,520 Now when we run this script We'll start with an empty collection every time. 39 00:05:00,960 --> 00:05:04,964 Let's run this file in the terminal and then check the database. 40 00:05:19,072 --> 00:05:24,818 There's the default animal, a goldfish, and an elephant. 41 00:05:37,015 --> 00:05:39,363 Let's save one more animal, a whale, 42 00:05:39,363 --> 00:05:42,290 to show that these defaults can be overwritten. 43 00:05:44,620 --> 00:05:53,080 Let's create a variable called whale and call the animal constructor. 44 00:05:59,780 --> 00:06:01,450 The type is whale. 45 00:06:06,858 --> 00:06:14,039 The size is big. 46 00:06:14,039 --> 00:06:21,100 The mass is 195,500 kilograms. 47 00:06:23,440 --> 00:06:24,011 Wow. 48 00:06:28,479 --> 00:06:30,043 And its name is Fig. 49 00:06:36,200 --> 00:06:40,121 Notice we're not supplying the color here. 50 00:06:40,121 --> 00:06:43,155 The whale should pick up the color from the defaults. 51 00:06:47,772 --> 00:06:51,218 Let's save it after we save our other two animals. 52 00:07:11,063 --> 00:07:17,510 Before we run this, let's also see how we can read animals out of the database. 53 00:07:17,510 --> 00:07:20,637 Let's ask the model for all big animals, and 54 00:07:20,637 --> 00:07:23,603 then write out their names to the console. 55 00:07:30,618 --> 00:07:38,597 The models find method takes in an object as its first parameter. 56 00:07:38,597 --> 00:07:43,329 This object should contain keys and values you want to match to your results. 57 00:07:50,294 --> 00:07:55,284 The second parameter is a callback function that passes in an error object 58 00:07:55,284 --> 00:07:57,547 and the results from your query. 59 00:08:18,966 --> 00:08:22,207 Let's print out each one of the results in the console now. 60 00:08:22,207 --> 00:08:31,753 console.log(animal.name +) " 61 00:08:31,753 --> 00:08:37,718 the " animal.color, 62 00:08:37,718 --> 00:08:43,688 +" " + animal type. 63 00:09:12,255 --> 00:09:14,681 Just a note here. 64 00:09:14,681 --> 00:09:18,510 You see this pattern we're building up with all these callbacks? 65 00:09:18,510 --> 00:09:23,780 This is a common pattern in Node, which you may have seen or experienced before. 66 00:09:23,780 --> 00:09:26,990 Manage an asynchronous calls by nesting them inside a callback 67 00:09:26,990 --> 00:09:28,610 after another callback. 68 00:09:28,610 --> 00:09:31,240 This is known as the pyramid of doom. 69 00:09:31,240 --> 00:09:34,200 But we're just playing around with Mongoose to just get a feel 70 00:09:34,200 --> 00:09:35,750 of how it works. 71 00:09:35,750 --> 00:09:40,320 Our code in the rest API we're going to build won't suffer from this as much. 72 00:09:40,320 --> 00:09:44,770 Special objects called promises can help us manage asynchronous code and 73 00:09:44,770 --> 00:09:47,880 make it easier for us to read and work with. 74 00:09:47,880 --> 00:09:51,620 Treehouse content that covers promises is in the teachers notes. 75 00:09:51,620 --> 00:09:54,533 However, we're not using promises in this course. 76 00:09:54,533 --> 00:09:58,275 Also, we'll clean up a lot of this code in the next video. 77 00:10:01,580 --> 00:10:03,680 In the terminal, run this file again. 78 00:10:07,043 --> 00:10:09,503 The data was successfully submitted and 79 00:10:09,503 --> 00:10:13,490 that the models were found that are big animals that we asked for. 80 00:10:14,740 --> 00:10:17,632 There we have the golden whale, Fig. 81 00:10:17,632 --> 00:10:22,010 Fig is golden because the default values were specified in the schema. 82 00:10:22,010 --> 00:10:27,640 So far, we've performed three of the four basic database operations with Mongoose, 83 00:10:27,640 --> 00:10:30,280 namely reading, writing, and deleting. 84 00:10:31,480 --> 00:10:33,660 Updating is a similar process. 85 00:10:33,660 --> 00:10:35,740 We'll cover that later. 86 00:10:35,740 --> 00:10:39,880 Now let's look at some more advanced things you can do with Mongoose.