Return to Video

Projection Queries - Data Wranging with MongoDB

  • 0:00 - 0:02
    Okay, one last thing I'd like to look at before
  • 0:02 - 0:04
    we move on to some more advanced query topics. Is the
  • 0:04 - 0:08
    ability to specify a projection document in addition to our query
  • 0:08 - 0:12
    document for calls to find. Now, what projection does is it
  • 0:12 - 0:15
    essentially describes the shape we would like documents to take in
  • 0:15 - 0:18
    the result set. I'm also going to break this out a
  • 0:18 - 0:22
    little bit here with the query and projection variable, and pass
  • 0:22 - 0:25
    those to find, instead of directly passing the dictionary that we
  • 0:25 - 0:28
    constructed to find. I point this out merely because we'll
  • 0:28 - 0:30
    see this later on in the lesson, and I don't want
  • 0:30 - 0:34
    anyone to be confused. Now, let's imagine that instead of getting
  • 0:34 - 0:37
    back all of the documents for our query specifying constraints on
  • 0:37 - 0:41
    manufacture and class, we actually are only interested in getting the
  • 0:41 - 0:44
    name of the document back. Well, we could simply print out
  • 0:44 - 0:47
    the name down here. But another way we can do that
  • 0:47 - 0:51
    is by specifying a projection as a second parameter to find.
  • 0:51 - 0:53
    And what that's going to do is say, all of the result
  • 0:53 - 0:58
    documents that come back, instead of having their full contents, I only
  • 0:58 - 1:02
    want to see name. No, by default, unless we explicitly say, I
  • 1:02 - 1:04
    don't want to see the ID field, we'll also get ID. So
  • 1:04 - 1:08
    that's why I've got ID set to zero here. Okay? So the
  • 1:08 - 1:11
    way we do this, is, essentially, we specify all the fields for
  • 1:11 - 1:14
    the documents in this collection. And then specify whether or not we
  • 1:14 - 1:17
    want them to occur in a result set. Alright? Let's run this.
  • 1:19 - 1:21
    Alright and here we can see that the documents
  • 1:21 - 1:23
    we get back simply have the name field in them.
タイトル:
Projection Queries - Data Wranging with MongoDB
Video Language:
English
Team:
Udacity
プロジェクト:
UD032: Data Wrangling with MongoDB
Duration:
01:24

English subtitles

改訂 Compare revisions