Skip to main content

MongoDB Example

Github StackBlitz

This project demonstrates how to connect to a MongoDB database in a Go application using the Fiber framework.

Prerequisites

Ensure you have the following installed:

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/mongodb
  2. Install dependencies:

    go get
  3. Set up your MongoDB database and update the connection string in the code.

Running the Application

  1. Start the application:
    go run main.go

Example

Here is an example of how to connect to a MongoDB database in a Fiber application:

package main

import (
"context"
"log"
"time"

"github.com/gofiber/fiber/v2"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
// MongoDB connection
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(ctx)

// Fiber instance
app := fiber.New()

// Routes
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, MongoDB!")
})

// Start server
log.Fatal(app.Listen(":3000"))
}

References