Introduction to MongoDB Introduction to MongoDB

Querying with Filters and Modifiers

Check out a free preview of the full Introduction to MongoDB course:
The "Querying with Filters and Modifiers" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:

Scott gives an overview of some ways that filters and modifiers could be used to manipulate entries.

Get Unlimited Access Now

Transcript from the "Querying with Filters and Modifiers" Lesson

[00:00:00]
>> Scott Moss: So is gonna show quick ways of viewing them. So in order to make this useful, I'm gonna add some more properties to a student, so we can see what this looks like or actually introduce school, school doesn't have anything. So school we can say openSince and we can put a number here.

[00:00:24] Then we can say how many students it has, we can put a number here. And we can also say isGreat, is it a great school? Hopefully it is. Okay. So now that we have that, what we can do is, I'm trying to comment some of this stuff out.

[00:00:43] If we have this school, and I'll just leave this findOneAndUpdate.
>> Scott Moss: I'm just gonna make another object so I'm not writing it twice.
>> Scott Moss: So if you have a school whose name is mlk.
>> Scott Moss: And it is openSince 2009, and students are.
>> Scott Moss: 1000 students. And is it great?

[00:01:32] You bet. Cool, so now that I have that, I got one school. And then I'm going to make another one.
>> Scott Moss: Which is basically the same thing with some small differences.
>> Scott Moss: Larry Middle school, I'm sure there's a Larry Middle school somewhere. And this was openSince 1980, has 600 students and it's not great.

[00:02:07] So what I want to do and we do got to get rid of these for a minute. As a first one we got to insert both of these into the database. I'm just going to say schools =, and the sweet thing about the create method on a model is that it can take an array of things to create, not just one.

[00:02:24] So I can pass in an array of school and school2, so schoolconfig and then school2. So it will create both of those long as they don't fail. If the second one messed up one of the validations on here, like if this was unique and this had the same name, then yeah, it just wouldn't work, it would just fail on that one.

[00:02:49] But the first one wouldn't get saved. So yeah, that's where transactions come in. And we're not gonna talk about transactions right now, it's complicated. So this will create two schools. So now we have those two, we can do some querying. So I'm gonna say, I wanna find the school,

[00:03:12]
>> Scott Moss: findOne. And then now we're gonna do some type of filter in here. So I wanna findOne where students is, and I'm gonna say greater than, so if I use this $gt, that's gonna give me greater than. So students have to be greater than 600 .exec. So if I do this, what should I get back?

[00:03:43] What school would I get back?
>> Student: MLK.
>> Scott Moss: Yeah, I get back MLK, that's the only one that's greater than 600. This one's exactly 600, so it's not greater than 600 so I should get back MLK.
>> Scott Moss: Cool, and I do, I get back MLK here. All right and you can get pretty crazy with this.

[00:04:07] So there's nothing stopping me from saying I wanna set a range. So it's also going to be less than some other number. And we'll put this on another line.
>> Scott Moss: And however many filters you wanna do. So I have students here, I can also start adding filters for openSince.

[00:04:28] Because that's a number, well, that's a pretty good example. I would do isGreat, so isGreat must be true, so I added a filter there. And as you can see, you can pretty much go on and on with these filters like that. Really, one that's kinda hard to conceptualize, at least for me it was, it kinda difficult cuz of Mongoose changes it all the time is when you deal with arrays and how you search for arrays.

[00:04:53] Like if there's a document inside of an array, how do I search for something inside of there? Well, I'm gonna show you. So if I add an array here of just staff, and it's just gonna be an array of names, so strings.
>> Scott Moss: And I'm just gonna add one like staff here, and they're just strings so they don't really have to be like, I'm just gonna make them all different.

[00:05:36]
>> Scott Moss: There we go. So they both have a similar b in them. And then I'm just gonna just not do that again. Okay, so now if I wanna be able to search what's inside of an array, it's actually pretty simple. So, I'm gonna get rid of these and then what I can do is I can just say staff, and then for the array I could just put whatever value I want b.

[00:06:00] And Mongoose is smart to know that staff is array and I'm searching for something inside of an array, so it's just gonna give me that. So if both of these have b in them, then I should get back both of these. So let's see what happens. And there's an explicit way to do this too, so right here.

[00:06:18] I did a findOne, so I only got one back. But if I did a find, I would've got both of them back, so let's change that. So a find and I don't want to key increment this cuz I don't wanna rewrite the code.
>> Scott Moss: There we go. So now we got a lot of them back.

[00:06:37] We got four cuz I run the script twice, right? So now we got four of them. As you can see like it's pretty powerful. So there's a lot of stuff that you can do there with the current, and it's pretty intuitive.
>> Scott Moss: Any questions on, yes.
>> Student2: If you wanted to search against multiple staff, would you use a regex to put in multiple names, or would you put an array of names?

[00:07:01]
>> Scott Moss: That's a good question. So if I wanted to do that, the way I would do that is I would use the in operator, $in. I think it goes first, if it doesn't go first there, we'll switch it. So I could say in staff, and then I could put an array of values, so I could say v, b, g.

[00:07:23] So that means, if any one of these schools has a staff with an array of one of these values, then return that. Because every school I has that then yeah, this will return everything. Whenever you see a dollar sign on an operator like that, that's a native Mongo operator, so native Mongo thing.

[00:07:45] Yes?
>> Student3: So SQL has selects top thousand or top ten records, so Mongo does have any operation of that kind?
>> Scott Moss: What was the operation?
>> Student3: So like select top hundred or select top ten.
>> Scott Moss: Got it-
>> Student3: Does Mongo has that?
>> Scott Moss: Yeah, so that's like a limit, it's like I wanna like only select this limit.

[00:08:09] Yeah so without sorting, yeah, you could just do .limit, like this. And I can say .limit(2), this will only give me the top two. But this isn't sorted so I mean you don't know which two you're gonna get back, but yeah. And if you did wanna sort it, you would do .sort.

[00:08:29]
>> Scott Moss: This allows you to sort by properties. So if I were to sort by descending openSince, I could put -openSince, like that, that'll sort descending. If I want ascending, I can split positive. If I don't want to use strings I can use an object and say openSince: 1, which is ascending or -1 for descending.

[00:08:52] It's very flexible.