1 00:00:00,630 --> 00:00:05,520 Before we move on to our next feature I want to give you a quick tip or two around how to effectively 2 00:00:05,520 --> 00:00:07,350 write queries with mongoose. 3 00:00:07,350 --> 00:00:12,030 So the first tip I have for you is going to seem very obvious but I want to just throw it out there 4 00:00:12,360 --> 00:00:14,860 just to make sure you understand this is definitely an option. 5 00:00:15,030 --> 00:00:20,730 So whenever you are trying to write some type of query with Mongoose to access or update some data inside 6 00:00:20,730 --> 00:00:21,520 and lango. 7 00:00:21,540 --> 00:00:26,400 The first tip I'm going to give you is to always open up a new tab in your browser and then just write 8 00:00:26,400 --> 00:00:29,280 out exactly what you're trying to do. 9 00:00:29,280 --> 00:00:35,410 So for example if we are trying to say update a document in a sub document collection. 10 00:00:35,520 --> 00:00:44,170 Well let's write mongoose G-S update document in some document collection just to ask the question. 11 00:00:44,190 --> 00:00:49,140 Just make sure you put like mongoose or mongoose J.S. Right in front of it so we can do a search for 12 00:00:49,140 --> 00:00:55,470 that and then hey here it looks like this looks like a reasonable link update and up find an update 13 00:00:55,470 --> 00:00:56,520 sub document. 14 00:00:56,530 --> 00:01:02,150 And so this is essentially exactly what we just did inside of our sub document collection of recipients. 15 00:01:02,190 --> 00:01:03,860 And so you can look at all the answers here. 16 00:01:03,870 --> 00:01:06,420 And sure enough it has the exact same methodology. 17 00:01:06,420 --> 00:01:13,510 We just used so every common operation inside of mongoose that you can possibly imagine. 18 00:01:13,530 --> 00:01:19,770 I can almost guarantee you there is an existing stack overflow question asking exactly how to handle 19 00:01:19,770 --> 00:01:21,350 it and how to approach it. 20 00:01:21,360 --> 00:01:27,350 So just about every operation you can imagine around mongoose has probably already been done. 21 00:01:27,510 --> 00:01:32,340 Do a search figure out how they did it figure out how they wrote their query and then add that code 22 00:01:32,340 --> 00:01:34,140 into your own. 23 00:01:34,140 --> 00:01:39,930 Now the next quick tip I have for you is probably a list a little bit less obvious and far more useful. 24 00:01:40,200 --> 00:01:45,510 So while this is useful of course but the other tip I can give you is a little bit more original I guess. 25 00:01:45,810 --> 00:01:52,500 So I'm going to flip back over to my code editor and I'm going to find my index file inside of my root 26 00:01:52,560 --> 00:01:53,910 server directory. 27 00:01:53,940 --> 00:01:56,540 So here's all of our startup code right here. 28 00:01:56,580 --> 00:02:03,630 So my next tip for you is a tip around how to rapidly write queries and test them out very quickly without 29 00:02:03,630 --> 00:02:09,450 having to wire up some request handler and then do some fancy requests logic to just trigger that code 30 00:02:09,450 --> 00:02:11,020 again and again. 31 00:02:11,070 --> 00:02:15,610 So I'm going to find everything from mongoose connect right here. 32 00:02:15,900 --> 00:02:19,230 I'm going to take this line and everything above it. 33 00:02:19,230 --> 00:02:24,300 I'm going to copy all of it and then going to flip on over to my terminal. 34 00:02:24,780 --> 00:02:31,020 I'm going to kill my running server and I'm going to enter the node command line interface by running 35 00:02:31,020 --> 00:02:33,840 the command node like so. 36 00:02:33,840 --> 00:02:39,510 So this kicks me into my node Selye so I can run any type of no japes code in here that I want. 37 00:02:39,510 --> 00:02:45,480 So like you know I have one plus one whatever or a concert be is color red. 38 00:02:45,510 --> 00:02:46,910 You know an object whatever I want. 39 00:02:46,920 --> 00:02:48,290 Now I could look at B. 40 00:02:48,660 --> 00:02:53,720 And so we can make use of this command line interface to easily write queries and test them out. 41 00:02:53,730 --> 00:03:00,050 All we have to do is require in some of the different files and folders and configuration from our project. 42 00:03:00,420 --> 00:03:07,650 And so I'm going to paste all the stuff that we just copied that required mongoose set up our models 43 00:03:08,970 --> 00:03:13,540 and then connected mongoose to our Mongo database right here. 44 00:03:13,560 --> 00:03:14,970 So if I paste all that stuff 45 00:03:17,700 --> 00:03:25,240 and all of a sudden I now have a connected instance of Mongo so I can say Konst survey is mongoose stock 46 00:03:25,290 --> 00:03:29,030 model surveys. 47 00:03:29,700 --> 00:03:32,810 And now I have my mongoose model class of survey. 48 00:03:32,850 --> 00:03:34,280 So I can say something like. 49 00:03:34,290 --> 00:03:38,570 Survey dot find and then pass in an empty object. 50 00:03:38,830 --> 00:03:43,320 And then this query right here would return all the different surveys inside my application. 51 00:03:43,320 --> 00:03:45,390 Now the one quick tip I have for you though. 52 00:03:45,390 --> 00:03:48,460 Remember that this is still an asynchronous operation. 53 00:03:48,690 --> 00:03:55,230 And so if I just execute this I won't actually see any results printed out in response unless I specifically 54 00:03:55,230 --> 00:03:56,700 attempt to console log them. 55 00:03:56,910 --> 00:04:02,430 So usually after writing out some test query like this we would say dot then and then we don't need 56 00:04:02,430 --> 00:04:04,130 to place a arrow function here. 57 00:04:04,140 --> 00:04:09,250 You can just say console log and you don't need to provide any parentheses to log. 58 00:04:09,270 --> 00:04:14,030 You're passing a reference to the console function to the price. 59 00:04:14,040 --> 00:04:19,170 So now if we execute this boom here we go here's our console log of all the different surveys that we've 60 00:04:19,170 --> 00:04:20,670 created over time. 61 00:04:20,670 --> 00:04:24,730 And so you can easily imagine how we can start to customize this query to test things out. 62 00:04:24,750 --> 00:04:35,390 So I'll say survey find Give me one with a title of Super final test like what I have right here so 63 00:04:35,390 --> 00:04:42,410 I'll paste that string and boom I get returned back that Servais with all the listed recipients inside 64 00:04:42,410 --> 00:04:43,460 of it. 65 00:04:43,460 --> 00:04:49,300 So again you can kind of use this Selye as the interface to rapidly write out in test queries just as 66 00:04:49,310 --> 00:04:49,970 a quick tip. 67 00:04:49,970 --> 00:04:55,550 One thing you might consider doing is writing out the query first over inside of your terminal. 68 00:04:55,550 --> 00:05:00,330 Let me get this over here are not your term but your code editor. 69 00:05:00,500 --> 00:05:04,490 And just because over here it makes things a little bit more legible and you can edit things a little 70 00:05:04,490 --> 00:05:05,740 bit more easily. 71 00:05:05,780 --> 00:05:10,280 And so I might sit over here inside of a new file and adjust my queery a little bit. 72 00:05:10,400 --> 00:05:17,690 You'll maybe find all of my surveys that have a yes or number of yes votes equal to zero. 73 00:05:17,810 --> 00:05:26,080 Very easy to make changes over here and I'll copy everything flip back over to the terminal and paste 74 00:05:26,490 --> 00:05:32,180 the new query and I forgot the dot after the or before the then there we go. 75 00:05:32,260 --> 00:05:37,290 Now I can get a list of all the different queries that I've never had a vote of yes applied to them. 76 00:05:37,290 --> 00:05:41,910 So again this is just a little tool that you can use to very quickly right out and test queries inside 77 00:05:41,910 --> 00:05:42,940 of your application. 78 00:05:42,960 --> 00:05:47,610 So I just want to make sure you were aware of this as a tool that you can use yourself. 79 00:05:47,610 --> 00:05:47,920 OK. 80 00:05:47,940 --> 00:05:49,730 So I think enough of the quick tips here. 81 00:05:49,770 --> 00:05:54,960 Let's continue in the next section and we're going to start to think about finally finishing up with 82 00:05:54,960 --> 00:05:55,770 this application. 83 00:05:55,770 --> 00:05:56,810 We're almost at the end. 84 00:05:56,820 --> 00:06:01,760 We really just got to get our big old list of surveys to appear on our screen for our users. 85 00:06:01,860 --> 00:06:04,720 So let's continue in the next section and start to tackle that. 86 00:06:04,740 --> 00:06:06,180 See you in just a minute.