0

I want to delete a property of a document ( in monngodb ) by using sth. like $pull/$unset. this operation needs to be handled by it's own route. Since the purpose of this route is to remove a property, I'm stuck between 2 HTTP methods DELETE and PATCH.

DELETE is used to delete whole documents while PATCH is mostly used to modify existing properties. My operation somehow has some of both properties so I'm wondering which HTTP method better fits this scenario. I couldn't find any description or discussions online. Would be nice to hear something from you folks

sami_analyst
  • 1,751
  • 5
  • 24
  • 43
  • Check this out https://stackoverflow.com/questions/28459418/rest-api-put-vs-patch-with-real-life-examples – kkkkkkk Oct 16 '18 at 10:39
  • @Khang yeah, kinda helps with better understanding the idempotency of PATCH but there is nothing about property deletion on properties. so this does not help much in my case – sami_analyst Oct 16 '18 at 10:58

1 Answers1

1

This is quite a subjective question, but I think it's still worth a crack. In my opinion you could do either, but you'll then want to make your request look like the chosen method.

Personally I would choose DELETE, thus I would make the URI target the document and then the docs property. This would effectively make your document property seem like a document itself. If you have the following document which represents an article / blog post.

{
  "id": "abcdefg",
  "title": "My fantastic article",
  "content": "This is my fantastic article that you're reading",
  "author_username": "elliotblackburn",
  "topic": "wonder"
}

To delete the whole document you might request DELETE /articles/abcdefg or to delete a property you might have DELETE /articles/abcdefg/topic where abcdefg represents the documents ID.

The fact that it is all the same document is an implementation detail, but as far as the user is concerned the "topic" can now be acted on as it's own document. This does not oblige you to implement all endpoints for the topic, but you may choose to.

Elliot Blackburn
  • 3,759
  • 1
  • 23
  • 42
  • I already implemented it this way, since I also had the same thought process :). Seems to be reasonable this way for me. – sami_analyst Oct 16 '18 at 11:15
  • You can also do it with PATCH but the question becomes "what does my json look like?" and at that point you're sort of looking at either doing a PUT to replace the whole object, negating the key you want to delete, or going down a bit of a SOAP route which of course isn't restful. – Elliot Blackburn Oct 16 '18 at 11:31