Introduction to MongoDB

Basic Query Methods

Scott Moss

Scott Moss

Superfilter AI
Introduction to MongoDB

Check out a free preview of the full Introduction to MongoDB course

The "Basic Query Methods" 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 demonstrates how to do several simple queries, such as finding a specific entry by field, finding an entry by ID, finding and updating by entry ID, and creating a timestamp field.


Transcript from the "Basic Query Methods" Lesson

>> Scott Moss: So now that we know that, there are gonna be a couple methods here that you can use. I'm just gonna walk through them briefly. So we've talked about create. The other ones are gonna be fine. So I'm gonna say student, I'm gonna auto complete this, nuts. Student.find and I have autocomplete set up here on VS Code, but you can see there are tons of methods here.

The ones that are gonna be very important to you are just gonna be find, which gives you back a list of things. And you could put whatever properties you wanna filter in on here. So if I wanted to find by first name, I would say find by firstname.

And whatever the first name was. And then it would try to find it. And that's gonna give you a list. Another one would be student.findById which exactly what it sounds like, you get an id know find one student by that id. So if I had an id here, I can type in here and then I would get back just one student with id if it exists.

Mongo doesn't throw errors if things don't exist. It just returns null. So if it's not there, it was just null. It's not gonna break.
>> Scott Moss: Other helpful ones that you will be using will be Student.findById and then like from there you can do delete, remove, update, pretty much all the CRUD operations.

And they're all pretty much the same. So if I wanted to do findByIdAndUpdate, I pass in the id as the first parameter. So like any id here. And then I pass what I want to have for an update. And then if it finds it, it'll push this object on to update it.

So these are just like basic crud methods but Mongoose has like so many of these methods it's ridiculous. And they keep changing them all the time and you can make your own but these are just the basic ones. So I'm just going to just label these.
>> Scott Moss: Make sure that code is good.

>> Scott Moss: Any questions on this course? Yes, Martin.
>> Speaker 2: In the find, is there a wildcard, like in SQL?
>> Scott Moss: [LAUGH] Yes, if you wanna do a wild card like give me everything, which you should not, then you just put an empty object. That's the wild card, it's like, just give me everything, just match everything, so just put an MD object there.

Let's go do the whole collection scan that's gonna scan your entire database. Well, I guess, technically it wouldn't scan it cuz it's gonna just drop the whole collection. But yeah, you'll probably just crash your server if you had a couple hundred thousand records or something. So yeah, just be careful with that.

But, yeah, that's how you would do that.
>> Speaker 3: Textual also? Textual wildcards part of?
>> Scott Moss: That's called a text index on a search. We probably won't get to that today because it's pretty advanced. But yes, you can set up an index on text so you can search within text.

Give me something that starts with F. And that's a text index, but that's pretty advanced.
>> Speaker 3: Because it's not common topic [LAUGH].
>> Scott Moss: Yeah, well-
>> Speaker 3: Well, for such honestly.
>> Scott Moss: Right, there's better tools to search than Mongo. It searches but not, [LAUGH] no. Yeah?
>> Speaker 4: Does MongoDB have default timestamps like-

>> Scott Moss: You guys just ask literally some of the best questions ever. By default, no, but just to show you, so we can get to this exercise, you can pass a second option to schema here, and you can put timestamps. True, and that'll put a created at and updated at timestamp for you.

>> Speaker 4: So Mongoose does that, MongoDB doesn't have that?
>> Scott Moss: Mongoose is doing this for us, yeah.
>> Scott Moss: Actually, Mongo probably supports it now, now that I'm thinking about it. They might support this as well. And if so Mongoose will pass it straight to them. But both of those libraries are in such a flux right now, because there is actually such a huge major release of Mongo version four, which released Mongoose version five.

So there's so many deprecated things happening right now, it's really hard to tell which ones doing which now. And if you look in their GitHub issues, there's just tons of what the hell's going on? So that's what's happening.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now