Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/azure-samples/cosmosdb-go-mongodb-quickstart

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/azure-samples/cosmosdb-go-mongodb-quickstart

  • v0.0.0-20200507112515-e8f05c320c2d
  • Source
  • Go
  • Socket score

Version published
Created
Source

page_type: sample languages:

  • go products:
  • azure description: "Build a Go application with MongoDB" urlFragment: "update-this-to-unique-url-stub"

Quickstart: Connect a Go application to Azure Cosmos DB's API for MongoDB

The sample application is a command-line based todo management tool written in Go. Azure Cosmos DB's API for MongoDB is compatible with the MongoDB wire protocol, making it possible for any MongoDB client driver to connect to it. This application uses the Go driver for MongoDB in a way that is transparent to the application that the data is stored in an Azure Cosmos DB database.

Prerequisites

  • An Azure account with an active subscription. Create one for free. Or try Azure Cosmos DB for free without an Azure subscription. You can also use the Azure Cosmos DB Emulator with the connection string .mongodb://localhost:C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==@localhost:10255/admin?ssl=true.
  • Go installed on your computer, and a working knowledge of Go.
  • Git.
  • If you don't want to use Azure Cloud Shell, Azure CLI 2.0+.

Setup

Clone the application

git clone https://github.com/Azure-Samples/cosmosdb-go-mongodb-quickstart

Change into the directory where you cloned the application and build it (using go build).

cd monogdb-go-quickstart
go build -o todo

To confirm that the application was built properly.

./todo --help

To configure the application, export the connection string, MongoDB database and collection names as environment variables.

export MONGODB_CONNECTION_STRING="mongodb://<COSMOSDB_ACCOUNT_NAME>:<COSMOSDB_PASSWORD>@<COSMOSDB_ACCOUNT_NAME>.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&maxIdleTimeMS=120000&appName=@<COSMOSDB_ACCOUNT_NAME>@"

The ssl=true option is important because of Cosmos DB requirements. For more information, see Connection string requirements.

For the MONGODB_CONNECTION_STRING environment variable, replace the placeholders for <COSMOSDB_ACCOUNT_NAME> and <COSMOSDB_PASSWORD>

  1. <COSMOSDB_ACCOUNT_NAME>: The name of the Azure Cosmos DB account
  2. <COSMOSDB_PASSWORD>: The Azure Cosmos DB database key
export MONGODB_DATABASE=todo-db
export MONGODB_COLLECTION=todos

You can choose your preferred values for MONGODB_DATABASE and MONGODB_COLLECTION or leave them as is.

Running the sample

To create a todo

./todo --create "Create an Azure Cosmos DB database account"

If successful, you should see an output with the MongoDB _id of the newly created document:

added todo ObjectID("5e9fd6befd2f076d1f03bd8a")

Create another todo

./todo --create "Get the MongoDB connection string using the Azure CLI"

List all the todos

./todo --list all

You should see the ones you just added in a tabular format as such

+----------------------------+--------------------------------+-----------+
|             ID             |          DESCRIPTION           |  STATUS   |
+----------------------------+--------------------------------+-----------+
| "5e9fd6b1bcd2fa6bd267d4c4" | Create an Azure Cosmos DB      | pending   |
|                            | database account               |           |
| "5e9fd6befd2f076d1f03bd8a" | Get the MongoDB connection     | pending   |
|                            | string using the Azure CLI     |           |
+----------------------------+--------------------------------+-----------+

To update the status of a todo (e.g. change it to completed status), use the todo ID

./todo --update 5e9fd6b1bcd2fa6bd267d4c4,completed

List only the completed todos

./todo --list completed

You should see the one you just updated

+----------------------------+--------------------------------+-----------+
|             ID             |          DESCRIPTION           |  STATUS   |
+----------------------------+--------------------------------+-----------+
| "5e9fd6b1bcd2fa6bd267d4c4" | Create an Azure Cosmos DB      | completed |
|                            | database account               |           |
+----------------------------+--------------------------------+-----------+

Delete a todo using it's ID

./todo --delete 5e9fd6b1bcd2fa6bd267d4c4,completed

List the todos to confirm

./todo --list all

The todo you just deleted should not be present

+----------------------------+--------------------------------+-----------+
|             ID             |          DESCRIPTION           |  STATUS   |
+----------------------------+--------------------------------+-----------+
| "5e9fd6befd2f076d1f03bd8a" | Get the MongoDB connection     | pending   |
|                            | string using the Azure CLI     |           |
+----------------------------+--------------------------------+-----------+

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

FAQs

Package last updated on 07 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc