简体   繁体   English

MongoDB只有一个项目,发布还是放置?

[英]MongoDB only one item, post or put?

POST to create a new item, PUT to update an item.. but what if there's only one item in the database? POST创建一个新项目,PUT更新一个项目..但是,如果数据库中只有一个项目该怎么办?

Do I code: 'if item already exists, find it and PUT' else 'POST' 我是否编码:“如果项目已经存在,找到并放置”,否则为“ POST”

or am I missing something very obvious? 还是我缺少一些非常明显的东西?

I briefly thought of doing post everytime, and on the first line of the post just delete everything, then post, but that seems dumb 我短暂地想到了每次都要进行发布,并且在发布的第一行只是删除所有内容,然后发布,但这似乎很愚蠢

Personally, I prefer to POST for the original creation, and PATCH for updates... 就个人而言,我更喜欢使用POST进行原始创建,而使用PATCH进行更新...


If you have the id, or can generate it on the client (such as a uuid): 如果您具有该ID,或者可以在客户端上生成该ID(例如uuid):

PUT /myresources/:id

If you don't have the id, or need to generate it on the server: 如果您没有该ID,或者需要在服务器上生成该ID:

POST /myresources

should return a location header with the generated path, as well as an acceptable response body. 应该返回带有生成路径的location标头,以及可接受的响应主体。

{
  id: "someid",
  location: "/myresources/someid"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM