MongoLab 通过 CURL/Golang-http 使用 HTTP PUT 删除多个集合不工作
MongoLab Delete Multiple collections using HTTP PUT via CURL/Golang-http not working
我正在尝试通过 HTTP 接口删除托管在 MongoLab 服务中的 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"
我基本上希望从集合中删除具有匹配查询的所有文档。
http://docs.mongolab.com/restapi/#view-edit-delete-document 的 Mongolab 文档建议 "Specifying an empty list in the body is equivalent to deleting the documents matching the query."
。但是我如何在 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)
它returns null
在这两种情况下(Golang 和 CURL 的 PUT 请求的情况)。如何让它工作以便删除所有匹配查询的文档?
我想您需要传递一个空的 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)
我正在尝试通过 HTTP 接口删除托管在 MongoLab 服务中的 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"
我基本上希望从集合中删除具有匹配查询的所有文档。
http://docs.mongolab.com/restapi/#view-edit-delete-document 的 Mongolab 文档建议 "Specifying an empty list in the body is equivalent to deleting the documents matching the query."
。但是我如何在 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)
它returns null
在这两种情况下(Golang 和 CURL 的 PUT 请求的情况)。如何让它工作以便删除所有匹配查询的文档?
我想您需要传递一个空的 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)