MongoDB with Async Python Transcripts
Chapter: Document Databases
Lecture: Queries: Projections

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The final thing I want to talk about in this native query chapter, we're almost done with it, is projections.
0:08 Now, projections are super, super important.
0:11 Maybe you've done SQL before and people said, ""Never, never select star. Always specify the columns you want back.
0:18 Well, you have a similar bit of advice in MongoDB and document databases, but it's even more important.
0:25 Why might people say, ""Don't select all the columns or parts of your data back""? because you might only need two things.
0:33 What if you only need the ISBN and the title? You don't care about when it was created, you don't care about the ratings,
0:39 well you're still pulling all that data back across the network and off of disk, potentially on the database server,
0:45 to just ignore it when you get to it. So a good practice is to just ask for what you're going to use. We get to Python, we'll talk about
0:53 when you don't want to do projections or when you do and how to do so in Beanie.
0:58 But in the native query syntax, the way you do that is there's a second parameter you can pass to find.
1:05 So in this case, we only care about the ISBN and title. So we say find whatever the query is, and then we project back ISBN title, ISBN title.
1:16 So we get just those back. Notice we get one other thing. We get the underscore ID, the primary key. By default, that's always going to come back.
1:25 However, all the other fields you have to opt in by passing something truthy. This case one will work. This is basically JavaScript.
1:34 If you want to suppress the ID coming back, you could say _id colon zero and then you
1:40 would literally just get back those two other fields, the ISBN and the title.
1:45 So this is really important because maybe we have a bunch of ratings and that's going
1:48 to slow down the response and we're not going to pay attention to them here. So forget them.
1:57 So projecting back a small subset of the document, it really helps performance.
2:03 I gave you some numbers earlier about talk by fun training, we have this number of queries and we have this amount of performance.
2:12 Very very carefully structuring the amount of data that comes back through projections on the server makes a huge difference there.
2:20 Let me just tell you a quick story about our apps. So our apps on the client side, these are native applications for iOS and Android.
2:29 On the server side, though, it's all Python, APIs, and MongoDB. Some of our APIs this app was talking to were taking as long as a second to respond.
2:40 Wow, what is going on with this? These used to be really fast. And I just don't understand why this part of our API started to slow down slower and
2:49 lower. It turns out that we were returning just the entire document for really, really
2:56 small bits of information, but we needed many of them. And so a slight change in how we
3:02 ran our query through a projection, we said, actually, we only need these two pieces of
3:07 data for this API response that we're querying here, not everything in the record. And those
3:14 went from taking like 900 milliseconds to respond down to 10 or 20 milliseconds. Incredible.
3:21 So projections, super, super important, more important than in relational databases, much
3:27 more important sometimes depending on your data model than relational databases.
3:31 Because if you go crazy and select star in relational databases, you'll get all the columns.
3:37 If you go crazy and select star in a document database, you might get whole chunks of other tables and relationships, right?
3:45 Remember, embedded objects, embedded records are kind of like a pre computed join.
3:50 If you don't want that part, project it out, and it'll be way, way faster. So that's it for our quick overview of the native view of MongoDB.
4:01 This is a kind of a for your information for your reference as you need it.
4:07 Chapter of information, you're not necessarily going to have to go write these projections by hand.
4:12 necessarily going to have to use those logical operators by hand, but if you need them, you can come back and find them.
4:19 Mostly, from now on, we're going to focus on Python code for talking to MongoDB.


Talk Python's Mastodon Michael Kennedy's Mastodon