MongoDB for Developers with Python Transcripts
Chapter: MongoDB's shell and native query syntax
0:01 Now sometimes you don't want all the data back,
0:03 usually it doesn't really matter to you if it comes back or it doesn't come back,
0:05 in the shell you're printing it out, it probably matters,
0:08 but in practice, in your app, you rarely care
0:10 from a display perspective or an interaction perspective,
0:14 whether some field or list that you are not using has data or not
0:19 but from a performance perspective, you very much may care.
0:22 Suppose that you have a document that's 50k in size
0:25 and all you want back is the isbn and the title and those are 1k,
0:30 and you're getting a bunch of them back,
0:32 it turns out that that can make a really big difference in terms of performance.
0:35 So whether it's for display purposes or it's for performance network purposes
0:39 using this second argument here we can say
0:44 only return the isbn and the title, and don't give me all of the ratings,
0:48 don't give me the images, everything else that might be in this book.
0:51 So we run this, and we get back these objects here, these documents,
0:56 and notice, we have the isbn and the title, like we asked for
0:59 but we also have the _id,
1:01 so unless you explicitly forbid the id from coming back
1:04 the id always comes, and everything else defaults to not appearing,
1:07 unless you indicate it if you pass some document here
1:10 for the projection or the restriction of things that come back.
1:14 If for some reason you don't want the id to come back,
1:16 just say_id:0 or false or something like this,
1:19 and then it will just have isbn and title exactly.