MongoLab使用HTTP PUT通过CURL/Golang-http删除多个集合不起作用。

huangapple go评论86阅读模式
英文:

MongoLab Delete Multiple collections using HTTP PUT via CURL/Golang-http not working

问题

我正在尝试通过MongoLab服务中的HTTP接口删除MongoDb数据库中集合中的多个文档。以下是我正在使用的CURL请求:

curl -H "Content-Type: application/json" -X PUT "https://api.mongolab.com/api/1/databases/mydb/collections/mycoll?q={\"person\":\"554b3d1ae4b0e2832aeeb6af\"}&apiKey=xxxxxxxxxxxxxxxxx"

我希望将与查询匹配的所有文档从集合中删除。Mongolab文档(http://docs.mongolab.com/restapi/#view-edit-delete-document)建议"在请求体中指定一个空列表等同于删除与查询匹配的文档"。但是我该如何在PUT请求的请求体中发送空列表呢?

以下是我用于实现上述PUT请求的Golang代码:

client := &http.Client{}
postRequestToBeSentToMongoLab, err := http.NewRequest("PUT","https://api.mongolab.com/api/1/databases/mydb/collections/mycoll?q={\"person\":\"554b3d1ae4b0e2832aeeb6af\"}&apiKey=xxxxxxxxxxxxxxxxx",nil )
postRequestToBeSentToMongoLab.Header.Set("Content-Type", "application/json")
responseFromMongoLab, err := client.Do(postRequestToBeSentToMongoLab)

无论是Golang还是CURL,都返回了null。如何使其正常工作以删除所有与查询匹配的文档?

英文:

Im trying to delete multiple Documents in a collection in my MongoDb database hosted in MongoLab Services via their HTTP interface . Here is the CURL request i'm using in trying to achieve it

 curl -H "Content-Type: application/json" -X PUT  "https://api.mongolab.com/api/1/databases/mydb/collections/mycoll?q={\"person\":\"554b3d1ae4b0e2832aeeb6af\"}&apiKey=xxxxxxxxxxxxxxxxx"

I essentially want all the documents with the matching query to deleted from the collection .
The Mongolab doc at http://docs.mongolab.com/restapi/#view-edit-delete-document suggests "Specifying an empty list in the body is equivalent to deleting the documents matching the query." . But how do i send empty list in PUT request body ?

Here is the Golang code im using to achieve the above PUT request

client := &http.Client{}
postRequestToBeSentToMongoLab, err := http.NewRequest("PUT","https://api.mongolab.com/api/1/databases/mydb/collections/mycoll?q={\"person\":\"554b3d1ae4b0e2832aeeb6af\"}&apiKey=xxxxxxxxxxxxxxxxx",nil )
postRequestToBeSentToMongoLab.Header.Set("Content-Type", "application/json") //http://stackoverflow.com/questions/24455147/go-lang-how-send-json-string-in-post-request
responseFromMongoLab, err := client.Do(postRequestToBeSentToMongoLab)

It returns null is in both the cases (case of PUT request by Golang and CURL ) . How to get this working so that it deletes all documents matching the query ?

答案1

得分: 0

我想你需要将一个空的 JSON 数组作为 PUT 消息的负载传递。使用 curl,可以这样写:

curl -H "Content-Type: application/json" -X PUT -d '[]' "https://api.mongolab.com/api/1/databases/mydb/collections/mycoll?q={\"person\":\"554b3d1ae4b0e2832aeeb6af\"}&apiKey=xxxxxxxxxxxxxxxxx"

在 Go 代码中,你需要写出类似这样的代码:

client := &http.Client{}
req, err := http.NewRequest(
    "PUT",
    "https://api.mongolab.com/api/1/databases/mydb/collections/mycoll?q={\"person\":\"554b3d1ae4b0e2832aeeb6af\"}&apiKey=xxxxxxxxxxxxxxxxx",
     bytes.NewBuffer("[]")
)
req.Header.Set("Content-Type", "application/json")
reply, err := client.Do(req)
英文:

I suppose you need to pass an empty json array as the payload of the PUT message. With curl, it would be something like:

curl -H "Content-Type: application/json" -X PUT -d '[]' "https://api.mongolab.com/api/1/databases/mydb/collections/mycoll?q={\"person\":\"554b3d1ae4b0e2832aeeb6af\"}&apiKey=xxxxxxxxxxxxxxxxx"

In the Go code, you need to write something like this:

client := &http.Client{}
req, err := http.NewRequest(
    "PUT",
    "https://api.mongolab.com/api/1/databases/mydb/collections/mycoll?q={\"person\":\"554b3d1ae4b0e2832aeeb6af\"}&apiKey=xxxxxxxxxxxxxxxxx",
     bytes.NewBuffer("[]")
)
req.Header.Set("Content-Type", "application/json")
reply, err := client.Do(req)

huangapple
  • 本文由 发表于 2015年5月8日 21:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/30125018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定