go

command module
v0.0.0-...-bae5d1d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2024 License: MIT Imports: 7 Imported by: 0

README

MongoDB logo

your MongoDB connection deserves a name: Go edition

An example on how to assign a name to a MongoDB connection in Go.

Get it running

  1. Start the MongoDB docker container:
$ docker run --rm \
    --publish 27017:27017 \
    --name ycdan-mongodb \
    --env MONGO_INITDB_ROOT_USERNAME=root \
    --env MONGO_INITDB_ROOT_PASSWORD=secret \
    --detach \
    mongo:5.0.1
  1. Compile the example program:
$ go build -o your-connection-deserves-a-name
  1. Start the example program:
$ ./your-connection-deserves-a-name

You should see something like

2021/07/30 18:46:37 Connecting to MongoDB on mongodb://root:secret@127.0.0.1:27017/
2021/07/30 18:46:37 Connecting to MongoDB on mongodb://root:secret@127.0.0.1:27017/ ... Successful
2021/07/30 18:46:37
2021/07/30 18:46:37 Keeping the connection open ...
2021/07/30 18:46:37 You can connect to the MongoDB server and execute the query:
2021/07/30 18:46:37 	db.currentOp()
2021/07/30 18:46:37 Alternatively, you should see the `appName` in the logs of the MongoDB server.
2021/07/30 18:46:37
2021/07/30 18:46:37 Hit CTRL + C or cancel the process to stop.
2021/07/30 18:46:37
  1. Login into your mongodb instance and execute the query:
db.currentOp()

In the key appName you can confirm your connection name:

Screenshot of the db.currentOp() query.

Alternatively, you can also check the server logs:

{
    "t": {
        "$date": "2021-07-30T16:30:18.042+00:00"
    },
    "attr": {
            "application": {
                "name": "currency-conversion-app"
            }
        }
    }
}

Don't know what this is all about?

Read the original blog post your database connection deserves a name @ andygrunwald.com.

Additionally, you can check out the projects README.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL