2015-05-08 81 views
1

我試圖通過它們的HTTP接口刪除MongoLab Services中託管的我的MongoDb數據庫集合中的多個文檔。這裏是我試圖實現它的CURL請求MongoLab通過CURL/Golang-HTTP刪除使用HTTP PUT的多個集合

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

我基本上想要從集合中刪除具有匹配查詢的所有文檔。 Mongolab doc http://docs.mongolab.com/restapi/#view-edit-delete-document建議"Specifying an empty list in the body is equivalent to deleting the documents matching the query."。但是,我如何發送PUT請求正文中的空列表?

下面是使用實現上述PUT請求

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) 

它返回null是在兩種情況下(由Golang和CURL PUT請求的情況下)的Golang代碼IM。如何獲得這個工作,以便刪除與查詢匹配的所有文檔?

回答

0

我想你需要傳遞一個空的json數組作爲PUT消息的有效載荷。隨着捲曲,這將是這樣的:

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) 
相關問題