Before we move on to our next feature I want to give you a quick tip or two around how to effectively
write queries with mongoose.
So the first tip I have for you is going to seem very obvious but I want to just throw it out there
just to make sure you understand this is definitely an option.
So whenever you are trying to write some type of query with Mongoose to access or update some data inside
and lango.
The first tip I'm going to give you is to always open up a new tab in your browser and then just write
out exactly what you're trying to do.
So for example if we are trying to say update a document in a sub document collection.
Well let's write mongoose G-S update document in some document collection just to ask the question.
Just make sure you put like mongoose or mongoose J.S. Right in front of it so we can do a search for
that and then hey here it looks like this looks like a reasonable link update and up find an update
sub document.
And so this is essentially exactly what we just did inside of our sub document collection of recipients.
And so you can look at all the answers here.
And sure enough it has the exact same methodology.
We just used so every common operation inside of mongoose that you can possibly imagine.
I can almost guarantee you there is an existing stack overflow question asking exactly how to handle
it and how to approach it.
So just about every operation you can imagine around mongoose has probably already been done.
Do a search figure out how they did it figure out how they wrote their query and then add that code
into your own.
Now the next quick tip I have for you is probably a list a little bit less obvious and far more useful.
So while this is useful of course but the other tip I can give you is a little bit more original I guess.
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
server directory.
So here's all of our startup code right here.
So my next tip for you is a tip around how to rapidly write queries and test them out very quickly without
having to wire up some request handler and then do some fancy requests logic to just trigger that code
again and again.
So I'm going to find everything from mongoose connect right here.
I'm going to take this line and everything above it.
I'm going to copy all of it and then going to flip on over to my terminal.
I'm going to kill my running server and I'm going to enter the node command line interface by running
the command node like so.
So this kicks me into my node Selye so I can run any type of no japes code in here that I want.
So like you know I have one plus one whatever or a concert be is color red.
You know an object whatever I want.
Now I could look at B.
And so we can make use of this command line interface to easily write queries and test them out.
All we have to do is require in some of the different files and folders and configuration from our project.
And so I'm going to paste all the stuff that we just copied that required mongoose set up our models
and then connected mongoose to our Mongo database right here.
So if I paste all that stuff
and all of a sudden I now have a connected instance of Mongo so I can say Konst survey is mongoose stock
model surveys.
And now I have my mongoose model class of survey.
So I can say something like.
Survey dot find and then pass in an empty object.
And then this query right here would return all the different surveys inside my application.
Now the one quick tip I have for you though.
Remember that this is still an asynchronous operation.
And so if I just execute this I won't actually see any results printed out in response unless I specifically
attempt to console log them.
So usually after writing out some test query like this we would say dot then and then we don't need
to place a arrow function here.
You can just say console log and you don't need to provide any parentheses to log.
You're passing a reference to the console function to the price.
So now if we execute this boom here we go here's our console log of all the different surveys that we've
created over time.
And so you can easily imagine how we can start to customize this query to test things out.
So I'll say survey find Give me one with a title of Super final test like what I have right here so
I'll paste that string and boom I get returned back that Servais with all the listed recipients inside
of it.
So again you can kind of use this Selye as the interface to rapidly write out in test queries just as
a quick tip.
One thing you might consider doing is writing out the query first over inside of your terminal.
Let me get this over here are not your term but your code editor.
And just because over here it makes things a little bit more legible and you can edit things a little
bit more easily.
And so I might sit over here inside of a new file and adjust my queery a little bit.
You'll maybe find all of my surveys that have a yes or number of yes votes equal to zero.
Very easy to make changes over here and I'll copy everything flip back over to the terminal and paste
the new query and I forgot the dot after the or before the then there we go.
Now I can get a list of all the different queries that I've never had a vote of yes applied to them.
So again this is just a little tool that you can use to very quickly right out and test queries inside
of your application.
So I just want to make sure you were aware of this as a tool that you can use yourself.
OK.
So I think enough of the quick tips here.
Let's continue in the next section and we're going to start to think about finally finishing up with
this application.
We're almost at the end.
We really just got to get our big old list of surveys to appear on our screen for our users.
So let's continue in the next section and start to tackle that.
See you in just a minute.