Skip to main content
Version: mongodb_v3.x.x

MongoDB

Release Discord Test

A MongoDB storage driver using mongodb/mongo-go-driver.

Table of Contents

Signatures

func New(config ...Config) Storage
func NewWithContext(ctx context.Context, config ...Config) *Storage
func NewFromConnection(client *mongo.Client, config ...Config) *Storage
func NewFromConnectionWithContext(ctx context.Context, client *mongo.Client, config ...Config) *Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) DeleteWithContext(ctx context.Context, key string) error
func (s *Storage) Reset() error
func (s *Storage) ResetWithContext(ctx context.Context) error
func (s *Storage) Close() error
func (s *Storage) Conn() *mongo.Database

Installation

MongoDB is tested on the 2 last Go versions with support for modules. So make sure to initialize one first if you didn't do that yet:

go mod init github.com/<user>/<repo>

And then install the mongodb implementation:

go get github.com/gofiber/storage/mongodb/v3

Examples

Import the storage package.

import "github.com/gofiber/storage/mongodb/v3"

You can use the following possibilities to create a storage:

// Initialize default config
store := mongodb.New()

// Initialize custom config
store := mongodb.New(mongodb.Config{
Host: "127.0.0.1",
Port: 27017,
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
})

// Initialize custom config using connection string
store := mongodb.New(mongodb.Config{
ConnectionURI: "mongodb://user:[email protected]:27017",
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
})

Config

type Config struct {
// Connection string to use for DB. Will override all other authentication values if used
//
// Optional. Default is ""
ConnectionURI string

// Host name where the DB is hosted
//
// Optional. Default is "127.0.0.1"
Host string

// Port where the DB is listening on
//
// Optional. Default is 27017
Port int

// Server username
//
// Optional. Default is ""
Username string

// Server password
//
// Optional. Default is ""
Password string

// Database name
//
// Optional. Default is "fiber"
Database string

// Collection name
//
// Optional. Default is "fiber_storage"
Collection string

// Reset clears any existing keys in existing Table
//
// Optional. Default is false
Reset bool
}

Default Config

var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1",
Port: 27017,
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
}

Using an Existing MongoDB Connection

If your application already holds a *mongo.Client, you can build the storage on it instead of connecting a second time. Only the Database, Collection and Reset options are read; the connection settings come from the client.

The client stays yours to disconnect: Close on a storage built this way leaves it connected, so the rest of your application keeps working. The storage itself is closed, and any operation on it afterwards returns ErrClosed.

import (
"context"

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

func main() {
client, err := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
defer client.Disconnect(context.Background())

store := mongodb.NewFromConnection(client, mongodb.Config{
Database: "fiber",
Collection: "fiber_storage",
})
defer store.Close()
}