Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Fine-tuning your REST service
Lecture: Client and server projections

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Projections are a powerful feature,
0:03 they allow a client to dictate of all document fields
0:05 which ones should be returned with the response.
0:11 In this request here we're asking for just the last name field
0:15 let's send it over and we can see that all the documents still have
0:21 the meta fields because these are always included
0:24 no matter the projection. We can also exclude fields,
0:27 for example, here I'm excluding the last name field
0:30 I want all the fields but not last name
0:33 and as you can see, here we're getting first name, location,
0:36 age, role, etc, but no last name.
0:40 Projections are nice because they allow the client
0:44 to pick only the fields it really needs.
0:47 Imagine if the people document had a picture
0:50 if we don't need a picture, we can just exclude it
0:55 and we won't be forced to download it, how nice is that?
0:59 You can, of course, disable projections
1:02 you simply go to the code editor in your settings file
1:05 and set projection to false.
1:10 This is global, and as you might expect by now
1:13 you can go and switch it on at local level if you want to.
1:24 So now projections are disabled at all endpoints but people.
1:29 Of course, we can also change the word used to query for a projection
1:41 let me save, relaunch, go back to Postman,
1:49 and use fields instead. Got it.
1:56 Now, so far we've have seen that the client can control projections
2:00 but we can actually predefine projections on server side.
2:05 Let's see how we do that.
2:10 Let's first clean up a little bit,
2:15 all right, we define server side projections at the endpoint level,
2:20 let me paste an example here,
2:23 data source is dictionary and it allows the projection key
2:29 do not confuse this projection key with root level projection boolean
2:33 which allows you to turn projection feature on and off.
2:38 So here we're setting a projection server side
2:41 every request sent to the people endpoint
2:44 will not include last name by default
2:47 let's try this, save, relaunch as usual and let's ask for people
2:58 without any client projection, same response.
3:03 No last name included with the response
3:16 as you can see, clients can still alter the output if they wish
3:19 but that the default is a different projection
3:23 it is a server projection,
3:26 default server side projection can come in handy in several situations
3:29 one is the picture we mentioned before,
3:32 you aren't letting the clients download it by default
3:36 but they can still go and require it with a specific projection should they need it.
3:41 Also keep in mind that projections are a read-only thing
3:46 they don't apply to write operation.
3:48 You still have a schema endpoint and clients can still write
3:53 the world document or edit the world document as they please.