question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PUT/PATCH don't support mongoose update operations

See original GitHub issue

This should work:

curl -XPUT /api/v1/User -d {"$addToSet": {"friends": "BoB"}}

but because we use document.set instead of document.update we loose the ability to use any of the mongodb update operators

currently the way that request is handled it results in doing the following operation: doc.save({'$addToSet.friends': 'BoB'})

instead of the desired: doc.update({'$addToSet': {'friends': 'BoB}})

What do you think of this proposed solution? not tested yet, but seems to work on my use-cases

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
b-grancommented, Jan 15, 2017

That’s a good point about deep endpoints.

For me, the key feature in opening up all of the MongoDB update operators is actually field deletion, because it’s not currently possible to remove a field from a document.

To perform PUT/PATCH ops, we’re using

context.findOneAndUpdate(
  {},
  { $set: body },
  { ... }
)

If we update PUT to completely replace the document, we would at least get field deletion via PUT, but we still won’t have deletion via PATCH (which will still need to use $set)

2reactions
norpancommented, Mar 14, 2016

If you decide to change this, please give consideration to the different semantics of PUT and PATCH. PUT is meant to replace a whole document. PATCH on the other hand is meant for just this use case.

There is a very good reason to separate PUT from PATCH. PUT is idempotent, which means that a client can assume that applying a PUT operation twice will give the same result as applying it once. So, while $addToSet happens to be idempotent, operations like $push are not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RESTful API, patch, mongoose update command doesn't ...
Patch method with mongoose update doesn't work instead just clears field which I try to update. I have tried update , updateOne ,...
Read more >
API with NestJS #49. Updating with PUT and PATCH with ...
Implementing the PUT method with MongoDB and Mongoose. There are quite a few ways of implementing a proper PUT method with MongoDB and...
Read more >
What's the Difference between PUT vs PATCH? - RapidAPI
PUT/PATCH – Update; DELETE – Delete. From this mapping, it is not surprising that most people think that PUT and PATCH are allies...
Read more >
Developing RESTful APIs with Hapi - Auth0
API should respond to common HTTP verbs, GET , POST , PUT , PATCH , DELETE ... We have defined the dog attributes...
Read more >
PUT/PATCH not going through when using postman + express
I'm trying to update a user's shipping information. I have default data in mongodb that ... .exports = mongoose.model("Shipping", ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found