MongoDB for Developers with Python Transcripts
Chapter: MongoDB's shell and native query syntax
Lecture: Projections
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Now sometimes you don't want all the data back, usually it doesn't really matter to you if it comes back or it doesn't come back,
0:06
in the shell you're printing it out, it probably matters, but in practice, in your app, you rarely care
0:11
from a display perspective or an interaction perspective, whether some field or list that you are not using has data or not
0:20
but from a performance perspective, you very much may care. Suppose that you have a document that's 50k in size
0:26
and all you want back is the isbn and the title and those are 1k, and you're getting a bunch of them back,
0:33
it turns out that that can make a really big difference in terms of performance.
0:36
So whether it's for display purposes or it's for performance network purposes using this second argument here we can say
0:45
only return the isbn and the title, and don't give me all of the ratings, don't give me the images, everything else that might be in this book.
0:52
So we run this, and we get back these objects here, these documents, and notice, we have the isbn and the title, like we asked for
1:00
but we also have the _id, so unless you explicitly forbid the id from coming back the id always comes, and everything else defaults to not appearing,
1:08
unless you indicate it if you pass some document here for the projection or the restriction of things that come back.
1:15
If for some reason you don't want the id to come back, just say_id:0 or false or something like this,
1:20
and then it will just have isbn and title exactly.