Docs Menu

Usage Examples

On this page

  • Overview
  • How to Use the Usage Examples

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

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 password, and <cluster-url> with the IP address or URL of your instance.

For more information about connecting to your MongoDB instance, see our Connection Guide.

To connect the example to your MongoDB instance, you must define an environment variable with your connection string.

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 GoDotEnv documentation.

MONGODB_URI=mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority
Note

Replace the preceding connection string with your MongoDB deployment's connection string.

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.