1

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 ?

Karthic Rao
  • 3,624
  • 8
  • 30
  • 44

1 Answers1

0

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)
Didier Spezia
  • 70,911
  • 12
  • 189
  • 154