YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Projection Queries - Data Wranging with MongoDB

Get Embed Code
4 Languages

Showing Revision 2 created 05/24/2016 by Udacity Robot.

  1. Okay, one last thing I'd like to look at before

  2. we move on to some more advanced query topics. Is the
  3. ability to specify a projection document in addition to our query
  4. document for calls to find. Now, what projection does is it
  5. essentially describes the shape we would like documents to take in
  6. the result set. I'm also going to break this out a
  7. little bit here with the query and projection variable, and pass
  8. those to find, instead of directly passing the dictionary that we
  9. constructed to find. I point this out merely because we'll
  10. see this later on in the lesson, and I don't want
  11. anyone to be confused. Now, let's imagine that instead of getting
  12. back all of the documents for our query specifying constraints on
  13. manufacture and class, we actually are only interested in getting the
  14. name of the document back. Well, we could simply print out
  15. the name down here. But another way we can do that
  16. is by specifying a projection as a second parameter to find.
  17. And what that's going to do is say, all of the result
  18. documents that come back, instead of having their full contents, I only
  19. want to see name. No, by default, unless we explicitly say, I
  20. don't want to see the ID field, we'll also get ID. So
  21. that's why I've got ID set to zero here. Okay? So the
  22. way we do this, is, essentially, we specify all the fields for
  23. the documents in this collection. And then specify whether or not we
  24. want them to occur in a result set. Alright? Let's run this.
  25. Alright and here we can see that the documents
  26. we get back simply have the name field in them.