How to use Azure Cosmos DB with Golang using MongoDB’s official Go driver

Throws: ", p.

Throws) i++}if err := cur.

Err(); err != nil { log.

Fatal(err)}//Close the cursor when we're Finishedcur.

Close(ctx)Update a documentWe can update a document using the UpdateOne method and the $set update operator.

You can read about other update operators here.

f := bson.

D{{"position", "2B"}}u := bson.

D{ {"$set", bson.

D{ {"position", "1B"}, }}, }ur, err := collection.

UpdateOne(ctx, f, u)if err != nil { log.


Println("Updated", ur.

ModifiedCount, "players")Delete a documentWe can also delete a single document using the DeleteOne method//Delete one_, err = collection.

DeleteOne(ctx, bson.

D{{"lastname", "Pujols"}})if err != nil { log.


Println("Deleted a single document")Or delete many documents using a filter and the DeleteMany method//Delete manydel, err := collection.

DeleteMany(ctx, bson.

D{{"bats", "Right"}})if err !=nil { log.


Println("Deleted ", del.

DeletedCount, "documents using delete many")Close the connectionOnce we’re done accessing our data we need to close the connection to thhe database//Close the connectionerr= client.

Disconnect(ctx)if err != nil { log.

Fatal(err)} else { fmt.

Println("Connection closed")}Run the appComment out the lines that delete the document, lines 217–229, so that you can see the documents after running the app.

Navigate to the directory that the sample is in.

From the gopath you can type the following in a terminal window to reach it:cd src/github.


In a terminal window build the app by typing the following command:go build4.

Run the app using go run:go run main.


You’ll see the results of the activity printed out to the consoleReview your document in Data ExplorerGo back to the Azure portal to see your document in Data Explorer.

Click Data Explorer (Preview) in the left navigation menu, expand your database, players, and then click Documents.

In the Documents tab, click the _id to display the document in the right pane.


You can then work with the document inline and click Update to save it.

You can also delete the document, or create new documents or queriesReview SLAs in the Azure portalThe Azure portal monitors your Cosmos DB account throughput, storage, availability, latency, and consistency.

Charts for metrics associated with an Azure Cosmos DB Service Level Agreement (SLA) show the SLA value compared to actual performance.

This suite of metrics makes monitoring your SLAs transparent.

To review metrics and SLAs:Select Metrics in your Cosmos DB account’s navigation menu.

Select a tab such as Latency, and select a timeframe on the right.

Compare the Actual and SLA lines on the charts.


Review the metrics on the other tabs.

Clean up resourcesWhen you’re done with your web app and Azure Cosmos DB account, you can delete the Azure resources you created so you don’t incur more charges.

To delete the resources:In the Azure portal, select Resource groups on the far left.

If the left menu is collapsed, selectto expand it.


Select the resource group you created for this quickstart.


In the new window, select Delete resource group.


In the next window, type the name of the resource group to delete, and then select Delete.

Thanks for reading and I wish you all the best on your journey with Azure and Golang!.

. More details

Leave a Reply