In the last section we put together a simple query to look through our survey collection and find all
the surveys that were created by the current user.
However we realized that if we stick with this query right here we'll also get the recipient list associated
with every single survey record and that might have several tens hundreds thousands of email addresses
associated with each one.
And clearly our dashboard component inside of our application doesn't really care about that big list
of emails.
So I think that we can probably figure out some way to tell mongoose that when we go out to find this
list of surveys do not return the recipient list and the response.
OK.
So we're going to look at the mongoose documentation to figure out exactly how to do this rather than
have me just tell you how to do it.
So I want you to get somewhat familiar with Mangus documentation.
So going to change on over to my browser and in a new tab we're going to search for mongoose.
J.S. Okay.
So here's longers OTM.
This is what we really want right here.
So a click that here's the mongoose home page.
We want to read the documentation.
And then this pops us over to the documentation for mongoose.
Now one quick note I want to throw out to you the documentation for Mongoose is really split into kind
of like two separate locations.
First on the left him Nav. over here you'll see this section called guide and underneath guide there
is a lot of documentation about very particular subjects and each of these are written like little articles
that you can check out and learn more about particular items.
So if I click on validation it will give me some plain English definitions about how validation works
with mongoose.
I can also get a better idea of how say queries work right here.
However if you want the real raw documentation that tells you about all the different particular methods
and functions that you can make use of then you want to scroll down to the very bottom of the left 10
Nav. over here and find the section called API docs so this is the kind of second location where all
the documentation is concentrated.
You've got that list of guides and then you've got this single one large API doc and I do mean large.
For example if I start to scroll down you'll see the scroll bar over here yeah.
It's a lot of stuff all on one page and kind of the bad side to that is it makes searching for documentation
side if you're kind of challenging.
There are some quick links down here on the left now that we can click on now that will tell you more
about very specific areas inside of the mongoose library.
But this kind of assumes you already know what you're looking for.
And so for us in this particular case you know we don't really know what we're looking for.
So it might be tough for us to find exactly the documentation that we want to look for.
And so again remember that we just spoke about some tips and tricks around mongoose.
Remember just about everything you could ever want to do around mongoose has a stack overflow question
around it.
So you can always do a nice quick search and say hey how do I do X Y Z with Mongoose and then hopefully
that might give you some keywords to search for in the documentation.
Now in this case I of course already done some research for us at a time and so I'm going to kind of
point us in the right direction to find the documentation that we want to reference in order to figure
out how we can tell mongooses not to return that list of recipients.
So on this API doc page I'm going to hit command F or control f to bring up the search interface.
Now we're going to do a search for pounde select and then you'll notice that there's six results here.
So I'm going to keep searching until I find queery pound select right here OK.
So if you've read the documentation says very simply which document fields to include or exclude and
then very importantly you'll also see this kind of exclusion or inclusion of different fields on a record
referred to as projection.
You'll see that term sometimes in the mongoose or Mongo divi documentation.
So the terms you know essentially this idea of including or excluding fields or white listing or blacklisting
fields or creating a query projection they're all talking about the same thing.
They're all saying when you go into the database and look for some data and pull it out.
This is a set of fields that we want to have be returned.
So essentially we're working with projections here so here is the documentation around how to do this.
And the way to read this head right here.
This says on the query class and then that pound right there indicates a high instance.
So there is an instance method called select.
So back inside of our application when we call survey done find right here.
This returns a query object from Mungo's.
And so after we do this initial query right here we can then train on as many additional aspects to
the queries we want.
So for example at the very end of this line or remove the semi-colon and then I can do a newline tab
in and say Dot select like so and then I can start to customize the query that was created on this first
line right here.
Now just a quick note.
We don't actually have to put this on a new line or indent it.
It could just as easily go on the line right above it like so but usually just to make these queries
a little bit more legible.
Will New Line it and indent it so it reads a little bit more like plain English as OK go through alerts
surveys find based on this criteria and then select these given properties inside of here.
OK so that's kind of how we read this thing right here in how we make use of the Select function to
pull off only specific properties.
So let's scroll down a little bit more and get a better example of how to use this thing.
OK.
So basically on the query object or whatever query we create we then say does select and if we want
to exclude specific fields we can pass in a string of dash and then the property name.
Now here's something really interesting about mongoose that personally I find really really irritating.
You'll notice that they give you one example right here of Oh yeah you can use a string to specify the
property names you do or don't want but then they also give you this alternate syntax down here where
they say oh but also you could just pass an object that specifies the properties that you want or the
properties you don't want with either 1 or 0.
And so to me it's kind of like you know why not just have one way or the other like either give us the
string method or give us the object method.
Otherwise it kind of just makes life a little bit unclear.
And you know if you're an engineer and you've only ever learned the string method for including fields
if you then see some code that uses an object you'll probably be sitting there saying like OK what's
going on here.
But you really know what's going on already is just this slightly different syntax that does the exact
same thing.
Anyways just one of my kind of pet peeves I guess I scroll in here or zoom in for one second because
I want to point something out in particular.
OK that's too much too much.
Sorry.
Let's go back to select.
OK so here's what we are just looking at.
So we're going to be going with this object syntax right here because I think it's just a little bit
more obvious what's going on.
But one thing I want to tell you is that the documentation says OK pass an object where the keys are
the properties that you want to either specify to include or not include.
And that shows a value of either 1 or 0 1 meaning include this property in the response or whatever
gets pulled out of the database and a zero meaning don't include this property.
So another kind of weird thing around the documentation here.
These ones and zeroes are really just a shorthand representation of true or false.
So you can either say a is one but you could just as easily say in like in the code we write you could
say a is true.
And I think using a true is way more clear what's going on there than saying select a of one like what
does one mean.
Right.
I mean it's another item where if another engineer comes on the scene they're going to say select a
of one why are we only finding results in this query where the property a is equal to one like that
doesn't make any sense.
So again another weird thing around the documentation here anyways.
Hey that's enough complaining to me let's just do a select statement.
OK.
So back over inside of our code editor we're going to find the select clause on here.
We're going to pass an object and then we're going to say specifically do not give us the list of recipients
so we'll say recipients false.
So do not include the recipient's property when we pull the list of surveys out of our database.
OK.
And I'll make sure that after the select statement I'll put on a semi colon and remember that on the
line before it we should not have a semi-colon and it looks like pretty or wants to put everything on
one line anyways whatever.
OK.
So I think that's about it for our API.
Slash surveys end points we are making sure that the user is logged in.
We specify that we're going to write some asynchronous code.
We reach our database find all the surveys created by the current user.
And then we specify that we do not want the result set to include the recipient's field.
So I think that this is looking pretty good.
Let's now continue in the next section where we will test out this point and we'll also get started
on the front end aspect of this feature.
So I'll see you in just a minute.