How it works…

Once we run the program, the HTTP server will start locally listening on port 8080

Next, executing a PUT request to UPDATE an employee document from the command line as follows will give you the number of documents updated in MongoDB:

$ curl -X PUT http://localhost:8080/employee/update/1?name=bar
Number of documents updated in database are :: 1

Let’s look at the program we have written:

  1. We defined an updateDocument handler, which gets the ID to be updated in MongoDB as a URL path variable and the new name as the HTTP request variable. As request variables are of string type, we have converted the variable ID of string type to int type. Then, we get the employee collection from MongoDB and call the collection.Upsert handler to insert if not present, or update an employee document with a new name for the supplied ID.
  2. Next, we registered an updateDocument handler to be called for the URL pattern /employee/update/{id} for every PUT request with gorilla/mux router and close the MongoDB session, using the defer session.Close() statement once we return from the main() function.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.15.99