MongoDB Go Driver Examples
Overview
The purose of this repo is for me to learn how to use the MongoDB Go Driver
and collate useful examples.
Requirements
Tool |
Description |
Docker |
This is used to launch the MongoDB container |
mongosh |
The MongoDB Shell |
MongoDB for VS Code |
This is used to create a playground for MongoDB |
MongoDB Container
For the purpose of a development environment, I'm using the mongo:latest
image
to launch a local development environment.
Run Docker MongoDB Container
docker-compose up -d
docker ps
docker volume ls
Shutdown the Docker Container
# Shutdown without deleting all containers
docker-compose stop
# Shutdown with deleting all containers
docker-compose down
VSCode MongoDB Extension
Please see the VSCode MongoDB extension
documentation to see how to connect via the extension.
The connection string is mongodb://root:rootpassword@127.0.0.1/
Below is an example of what the connected database will look like via the
extension:

Running the Sample Go Scripts
To tun the scripts, run the following command:
go run cmd/cli/main.go
You'll be prompted asking if you want to populate the DB with sample data. This
only need to be done on the first run. You can see an example of this below:

Appendix
Connect with Mongosh
mongosh admin -u root -p rootpassword