Introduction to MongoDB

Create a Document on POST Request

Scott Moss

Scott Moss

Superfilter AI
Introduction to MongoDB

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

The "Create a Document on POST Request" 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 live codes the API server to post a note, and return the note after it's created.


Transcript from the "Create a Document on POST Request" Lesson

>> Scott Moss: And then if I wanted to create a note, so if I did a post request to ('/note', asynch(req, res). First thing is I'll do is I'll get the note that is trying to be created. So in Express, we will get that on the request object. So I'll say notesTobeCreated = req.body.

So if you do an Ajax call to the server, whatever you place in the body will be added to this object req.body. That's how Express works. Other frameworks and languages are very similar. So I got the note to be created and then I'll just create it. So notes = await Note.create(noteTobeCreated) and that's it.

And then I'll just say res.status, it's a post, so I'll be a good engineer and do a (201) for a post. And then I'll send back the note, and that's it. So I created a note from the object that was sent to the server, and then I send it back after I created 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