MongoDB for Developers with Python Transcripts
Chapter: High-performance MongoDB
Lecture: Concept: Projections

Login or purchase this course to watch this video and the rest of the course contents.
0:01 One of the last simple tools you have in your tool belt
0:04 when we're working with MongoEngine or even in PyMongo, just different api
0:08 is this ability to restrict the data returned from the document.
0:13 In our car object we've got the make, the model, the id, some other things,
0:17 we've got the engine which is a subdocument or an embedded document there
0:22 and then the biggest thing that contributes to the size
0:25 is actually the service history which might be many service record entries.
0:30 If really all we care about is the make, the model and the id of a car,
0:34 and we're going to create like a list or something like that,
0:36 we can use this .only operator here
0:39 and dramatically reduce the amount of data returned from MongoDB
0:43 so this is an operation that we saw when we first learned about the api
0:46 actually operates at the database level,
0:48 you're able to restrict the elements returned from the queries
0:52 so when it gets back to MongoEngine
0:54 basically it looks at what comes back and it says,
0:57 alright, I need to create some cars
0:59 and I need to set their make to this, the model to that
1:01 and their id to whatever comes back,
1:03 and then nothing else is transferred, deserialized, anything.
1:05 So you can, if you don't need them, exclude the heavyweight things
1:09 like the engine and the service histories for this particular use case.
1:12 So this is kind of like select make, model, id from table such and such in SQL,
1:20 and it really can improve the performance
1:22 especially when you have either large documents or many documents.
1:27 So you've seen a lot of different ways to turn the knobs of MongoDB
1:31 to make it faster and to use MongoEngine to control those knobs.
1:35 Now this applies to a single individual database server
1:38 and if you use this to tune your database,
1:41 you can actually make the need for having a sharded cluster
1:45 and all these scaling things possibly go away,
1:48 but even if you do end up with one of these more interesting topologies,
1:52 all of these techniques still apply and they'll make your cluster go faster,
1:56 they'll make your replicas go faster, all of those things.
1:59 What you've learned here are really the foundational items of making MognoDB go fast.