Usage examples provide convenient starting points for popular MongoDB operations. Each example provides the following:
- A full Go program that you can run in your own environment
- The expected result after running the program
How to Use the Usage Examples¶
These examples use the sample datasets provided by Atlas. You can load them into your database on the free tier of MongoDB Atlas by following the Get Started with Atlas Guide or you can import the sample dataset into a local MongoDB instance.
Once you import the dataset, you can copy and paste a usage example into your development environment of choice. You can follow the quick start guide to learn more about getting started with the MongoDB Go Driver.
Follow the "Connect to your Cluster"
step to find the
connection string to define your
MONGODB_URI environment variable to run our usage examples. If your
instance uses SCRAM authentication,
you can replace
<user> with your username,
<password> with your
<cluster-url> with the IP address or URL of your instance.
For more information about connecting to your MongoDB instance, see our Connection Guide.
uri := os.Getenv("MONGODB_URI")
You can use GoDotEnv to define your environment variable.
Add the following application configuration in your
.env file at the
root of your project. For more information, see the
Replace the preceding connection string with your MongoDB deployment's connection string.