Introduction to MongoDB

Querying Q&A

Scott Moss

Scott Moss

Superfilter AI
Introduction to MongoDB

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

The "Querying Q&A" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:

Questions are asked about the ._id property, adding another document to the existing parent document,


Transcript from the "Querying Q&A" Lesson

>> Student: Could you go to the end of that line?
>> Scott Moss: Which one?
>> Student: 39, the one you're on.
>> Scott Moss: There you go.
>> Student: So why do we use school._ id here but then on 41 we use
>> Scott Moss: That's a good question. So remember, these queries and these Mongoose Operations return Mongoose objects.

And part of that, there's this thing called virtualization, that you're gonna be working on soon. And virtualization's basically, it's a field that doesn't exist in a database but gets created at run time. And by default, there is an _id that saves at a database. That's kind of annoying to do _id all the time.

So mongoose virtualized the field called id. That represents the same thing as underscore ID, but that's only if you use the Mongoose objects. If you were to convert that to JSON or object by default, you won't have .id. So just to be explicit and to avoid errors, it can be saved as use _ id until you understand virtuals and how to keep those consistent.

>> Student2: So if you add another student to the school?
>> Scott Moss: Yes, so if I add another student to the school or create another student that has reference to the school. So we can say, const student2, and I'm just gonna copy that, change the name.
>> Scott Moss: And it's got the same school.

>> Scott Moss: All right, so I'm gonna do that and then,
>> Scott Moss: Actually, I gotta change these two cuz those will break again.
>> Student2: Or could it do just create or find, or something like that?
>> Scott Moss: Create or, you mean this one? You like if I change this to create or, are you talking about for the school or for the student?

>> Student2: Yeah, for the school.
>> Scott Moss: For the school. You mean like, if it is already there don't do it. Is that what you are saying? Yeah, you can do that. So I will do, create. Actually, you don't have to create that there. I think they got rid of that.

I'm almost positive they got rid of that, hold on.
>> Scott Moss: Yeah, I think you might be referring to find one and update with true. Whereas like it finds it. If it does find it, it will return it. If it doesn't see it, I can put upsearch through, which we'll create it.

I think that might be the one you might referring to. So if we do that, that's basically this. So stop doing create. But you'd have to be there and let's. So it'll be school.findOne, and then I could say mlk elementry and then findOneAndUpdate. So I have to do that.

But if it's not there, then I have to put the same object in there that's for the find.
>> Scott Moss: Like that, just in case it's not there. And then I can say upsert: true, which will create it if it's not there. And then just to make sure I get the new one back, new true, avoid that.

So that's saying find it. If it is there, return it. If it is there, update it. If it's not there, create it from scratch with this second object. So whatever the second object is, the updated object, that's what I want you to create it from. So that's how you would avoid that error.

>> Scott Moss: Any other questions?
>> Student3: Do we have exec at the end of 38 or not? Do I not need that?
>> Scott Moss: If there's a find, you definitely wanna put an exec. I haven't been using exec on any of these. Haven't been explicit because I'm not doing anything with them.

But, yeah, always get in a good habit of doing .exec on them. Like I said, you probably won't run into issues with them. But if you use a library that is like very strict with promises and they are expecting a promise spec promise, then you're going to run into a problem.

But if you're just using something like .den, then you're never going to run into a problem. But there are promise libraries out there that are to the spec. And if the spec ever evolves into something else, other than just .den, then these will all break.
>> Student2: So upsert either updates or creates it.

>> Scott Moss: Yeah, so upsert means if you don't find a matching document with this query, then create a new document with this update. That's what upsert does.
>> Student2: What is new: true for you?
>> Scott Moss: New: true, what it does is, by default, if you update a document, it's not gonna return the new document that was updated.

It's gonna return the document before that was not updated. I don't know why it would ever do that. So you have to pass new: true to say, give me the document after you updated it so I have the new properties versus the document before you updated it. The only other stuff I want to show you that's going to be helpful for your exercise is walking through some more slightly complicated queries.

So we already talked about populate but there is some other stuff that we can do in here. For instance, if we have these students, I have a student whose first name is Trisha. Then what I can do, instead of say findById, let's say I just say findOne. Now, I can pass in an object and I can say firstName the same as Trisha, and that one might be obvious.

That one might be pretty obvious that obviously we're doing name match here for field. But there's a lot more stuff you can do in here, and it's pretty optional how you want to do them. So it really depends if you want to run a Mongoose query, which is methods that are chainable, or if you just wanna pass filters as properties here.

So you can kinda go both ways of how you wanna do it.

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