This course has been updated! We now recommend you take the API Design in Node.js, v4 course.

Check out a free preview of the full REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course:
The "Solution: Querying MongoDB" Lesson is part of the full, REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course featured in this preview video. Here's what you'd learn in this lesson:

Learn how to query MongoDB. Scott shows how to find by ID, find by name, create and update documents and more.

Get Unlimited Access Now

Transcript from the "Solution: Querying MongoDB" Lesson

>> Scott Moss: So to everybody, was everybody, who was not able to go through and get these done? I got one. Anybody else? No, everybody else is good, okay, cool, let's work through it then. Let's see, let me check out.
>> Scott Moss: I'm gonna do this stash.
>> Scott Moss: Okay, so let's see what we got here, let me clean this up going.

>> Scott Moss: Boom, okay. So we did the create one already, that's just going to return the model.create. Now remember this model right here is gonna be the model that's gonna be passed hit, right? Like I said this meta programming, so it can get very difficult to follow what's going on, and having code generate other code.

[00:00:55] But if you ever get lost then you can come in here and throw a simple log in here. Log that model, go over to the spec, that was on create. You could put a .only right here. Only run this test.
>> Scott Moss: And you can run your own test, and you can see what that model is, and you should see like four logs.

[00:01:15] Each one with a different model, so you can see here each one of these is a model. If you were to read it a little carefully you might even see the name in here somewhere. Like for instance, this is the user one you can see right there.
>> Scott Moss: There's a lot of stuff going on here.

[00:01:30] But yeah, so you can check that out, see what's going on, see what the model is. But we already know what's going on, so we're gonna head back.
>> Scott Moss: And there we go, so we got the createOne. This is just gonna call model.create, this is a Mongoose model.

[00:01:44] We're gonna call the create method on it with the given body, and then updateOne. So we already have the document to update, we already have it in memory. The find one by params when we go make it below it's going to do that for us. So we already have a document, and then we have the update on which we want to contribute to this document.

[00:02:04] We want this document to have these new properties, these new values I mean on these properties. So we use the merge, function from the top lodash.merge. Which is basically just like extend, except for it's recursive. It does every single property, whereas extend just does only top level properties.

[00:02:19] So if you ever use object.extend, or I mean, we're in ES6. So you can actually do like this, right, with objects? That's not what we want, we wanna be able to go down fully, so I'm using merge to do that. And the way merge works is, the first objects you pass in, it's gonna inherit the values from the ones to the right of it, right?

[00:02:40] So that's why I'm not looking at the return value of this, cuz it's actually mutating this. This is a mutation, it's not a stateless operation. If I were to do this, then it is stateless, and now I need to reference this thing.
>> Scott Moss: All right, but because I'm doing this, it's totally fine.

[00:02:59] We're good to go. And now I can just call it .save on that document, because I enhanced it. I mixed in these new property values. So we call it .save.
>> Scott Moss: Delete one, a document knows how to delete itself with a .remove method. So you can call .remove on the document.

[00:03:18] A document to get like I said this one's like, a trick question, you just return the document with a promise. This seems trivial but our example is one, the reason this is happening is one, we wanna keep everything using promises down here, because that's just what we're doing for everything.

[00:03:32] And at the same time, this is an opportunity to do other things here, if you wanna do other things, you do it right here.
>> Scott Moss: GetAll just does find on the model with literally no criteria on the search. So I put an empty object, as in, there's no search criteria, find every single thing for this model.

[00:03:51] Find me every song, find me every whatever. That's what this is doing.
>> Speaker 2: So earlier [COUGH] in the lesson you said anything that starts with find, you .exec?
>> Scott Moss: That's right, need a .exec there. There we go. And findByParam is findByID which also needed .exec. This takes in the model and the ID so we're just gonna need findByID.

[00:04:14] On that model, pass in the ID, and then we'll either get it or we won't.
>> Scott Moss: And if we run the test,
>> Scott Moss: If I get rid of that, [INAUDIBLE]
>> Scott Moss: Our test should by passing, so you should have 12 passing tests. Does everybody have 12 passing tests?

[00:04:40] And if not which test is failing, so we're gonna fix it. We're good, okay.