Home > Mobile >  Does it matter if you use POST or PUT when updating a Mongoose Subdocument in MongoDB?
Does it matter if you use POST or PUT when updating a Mongoose Subdocument in MongoDB?

Time:10-03

Does it matter if you use POST or PUT when updating a Mongoose subdocument in MongoDB? Originally I was using PUT to update, but it wasn't working with an API I added in later, so I changed it to POST and it appears to work for updating the subdocument. So does it really matter which you use with MongoDB? I'm guessing PUT has plenty of uses in other cases.

Schemas for reference:

const hikeSessionSchema = new mongoose.Schema({
    hike_name: {type: String, required: true}
  })

const hikerSchema = new mongoose.Schema({
  email: {type: String, required: true},
  password: {type: String, required: true},
  log: [hikeSessionSchema]
})

CodePudding user response:

Actually no difference between POST and PUT for mongoose. But in web API those method have different.

Im rest API POST for use add something for example add new user in web server and PUT use for update something like update user info

But actually no difference between POST and PUT in http, It's just standard and you can add ad update with POST

If you're interesting about that topic you can read below document: https://www.restapitutorial.com/lessons/httpmethods.html

CodePudding user response:

1

Either one should be possible.

Zooming out, let's think about why this is the case by thinking about what happens at a high level.

  1. A client sends an HTTP request to a server.
  2. The server receives the request.
  3. The server parses the request (eg. such that req.method is POST).
  4. The server decides what code to execute (eg. app.post('/foo/bar', handler)).
  5. The server executes that code (eg. handler()).

Roughly. The big point here is that in step 4, it is the server that decides what to do. The request could be DELETE. It doesn't matter. The server can decide that when it sees a DELETE request, it's going to create a record. Now, that is probably a bad idea. My point is just that it is possible.

With that said, let's think about why it is only working with POST but not PUT. You'll have to look through your server code and check out how the routing is done (perhaps with Express?). There is going to be some routing logic that is looking for a POST request. You can change it to look for PUT instead if you would like.

To be clear, the answer will lie in your server's routing logic. It actually has nothing to do with Mongoose. Mongoose lives in step five of my outline above, whereas the answer to your question lives in step four.

2

Let's ask the question of whether it should be PUT, POST, or maybe something else.

  • POST is for creating records.
  • PUT is for replacing entire records.
  • PATCH is another option available for updating a portion of an existing record.

So if you have a User record with a username, email and password, if you are sending over { username: "johndoe", email: "[email protected]", password: "020392" } as the request payload and replacing the existing record with those values, then that would be a PUT request. But if you only want to change, let's say the email, then you could send something like { email: "[email protected]" } to the server and only update that email field with Mongoose, and use a PATCH request for that. Check out Use of PUT vs PATCH methods in REST API real life scenarios for more information on PUT vs PATCH.

  • Related