Transcript from the "Projections" Lesson
>> So let's talk about projections. What's fun about these is, you're gonna learn all these concepts once and then we're gonna go to the next database and a lot of these concepts being almost exactly the same. So projections exist in all of these different databases. It's probably something you've actually done before and you didn't know that there was a term for it.
[00:00:18] So let's say we wanted to go find all the dogs but we only wanted to see their names. We don't actually wanna see anything else about them. We just wanna return their names, we use something called the projection to do that. So I'm gonna say db.pets.find. I'm gonna say type, dog.
[00:00:34] And we're gonna end that object. We're gonna provide a second parameter to this find query here. And all we wanna do is get the name. So we're gonna say name. And here you can put a lot of things. You can put true. You can put 1. I end up normally putting 1.
[00:00:59] I think that's just from habit. But whatever you want to include in your results, you just put that. And I'm just gonna say here limit 5 so we don't get a tonne of things back. And you can see here, all we're getting back is a name, whereas up here we're getting the whole object back.
[00:01:16] Now, here's a bit of advice for you when you're writing your particular database queries. Do the projections, right? Only include the things that you need for your query, don't include other things. It's a security thing, which is useful that if someone can get into your app server, they can't see data flowing in and out of it.
[00:01:35] You'll notice that this is just smaller in general, right? So you'll end up using less bandwidth. It's just a good idea to always only include the things that you need, don't include things that you don't need. So even in this particular case, notice it's included the ID. It does that by default no matter what.
[00:01:50] If I want to not include that, you can actually come in here and specifically disallow that. So if I put negative -1 there or false, both of those would work. You can see. It should not include that. It's 0, it's not include. So there you go. That way, we actually even disinclude the _ID as well.
[00:02:20] So just to prove my point here, you can also say name: true, breed: true, I'm gonna do ID: false. So all of those work as well. And another thing that you can do as well, you can actually just disinclude things. So if I do ID false like that, it'll include everything else.
[00:02:44] So that's kind of takes a disallow list kind of approach to projection as well that is like, specifically don't include these things, include everything else. So let's say you're querying a user database that has their password hash in it, you could specifically just disinclude that if you don't need that for whatever you're doing.
[00:03:01] So that's a lot of what's about querying MongoDB. So at this point, you're probably pretty well equipped to just go out there and start reading from MongoDB. The nice thing about learning these databases is 85% of the use case can be learned in about, I don't know how long we've been talking about now, or something like that.
[00:03:21] And then there's a very long tail of really wild stuff that they can do. So we'll talk a little bit about the wild stuff. But for the most part one, you'd to be familiar with all these different databases so that you can make intelligent decisions about which one you wanna use for your app.