id: mongodb
title: MongoDB
A MongoDB storage driver using mongodb/mongo-go-driver.
Table of Contents
Signatures
func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() 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
Examples
Import the storage package.
import "github.com/gofiber/storage/mongodb"
You can use the following possibilities to create a storage:
store := mongodb.New()
store := mongodb.New(mongodb.Config{
Host: "127.0.0.1",
Port: 27017,
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
})
store := mongodb.New(mongodb.Config{
ConnectionURI: "mongodb://user:password@127.0.0.1:27017",
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
})
Config
type Config struct {
ConnectionURI string
Host string
Port int
Username string
Password string
Database string
Collection string
Reset bool
}
Default Config
var ConfigDefault = Config{
ConnectionURI: "",
Host: "127.0.0.1",
Port: 27017,
Database: "fiber",
Collection: "fiber_storage",
Reset: false,
}