mon_go

package module
v0.0.0-...-3203493 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 6 Imported by: 0

README

mon-go

barebones MongoDB client wrapper for go (driver v2)

full example

Install

go get github.com/benjamint08/mon-go

Usage

package main

import (
	"context"
	"log"
	"time"

	"github.com/benjamint08/mon-go"
)

func main() {
	client, err := mon_go.New(mon_go.Config{
		URI:            "mongodb://localhost:27017",
		Database:       "appdb",
		ConnectTimeout: 5 * time.Second,
		PingTimeout:    2 * time.Second,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer client.Disconnect(context.Background())

	users := client.Collection("users")
	_ = users
}

CRUD Examples

package main

import (
	"context"
	"log"
	"time"

	"github.com/benjamint08/mon-go"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type User struct {
	ID        string    `bson:"_id,omitempty"`
	Email     string    `bson:"email"`
	Name      string    `bson:"name"`
	CreatedAt time.Time `bson:"created_at"`
}

func main() {
	ctx := context.Background()

	client, err := mon_go.New(mon_go.Config{
		URI:      "mongodb://localhost:27017",
		Database: "appdb",
	})
	if err != nil {
		log.Fatal(err)
	}
	defer client.Disconnect(ctx)

	users := client.Collection("users")

	// Create
	_, err = users.InsertOne(ctx, User{
		Email:     "a@example.com",
		Name:      "Ada",
		CreatedAt: time.Now().UTC(),
	})
	if err != nil {
		log.Fatal(err)
	}

	// Read (one)
	var found User
	if err := users.FindOne(ctx, bson.M{"email": "a@example.com"}).Decode(&found); err != nil {
		log.Fatal(err)
	}

	// Read (many)
	cur, err := users.Find(ctx, bson.M{}, options.Find().SetLimit(10))
	if err != nil {
		log.Fatal(err)
	}
	defer cur.Close(ctx)
	for cur.Next(ctx) {
		var u User
		if err := cur.Decode(&u); err != nil {
			log.Fatal(err)
		}
	}
	if err := cur.Err(); err != nil {
		log.Fatal(err)
	}

	// Update
	_, err = users.UpdateOne(
		ctx,
		bson.M{"email": "a@example.com"},
		bson.M{"$set": bson.M{"name": "Ada Lovelace"}},
	)
	if err != nil {
		log.Fatal(err)
	}

	// Delete
	_, err = users.DeleteOne(ctx, bson.M{"email": "a@example.com"})
	if err != nil {
		log.Fatal(err)
	}
}

Env Setup

NewFromEnv needs:

  • MONGO_URI
  • MONGO_DB
package main

import (
	"context"
	"log"

	"github.com/benjamint08/mon-go"
)

func main() {
	client, err := mon_go.NewFromEnv()
	if err != nil {
		log.Fatal(err)
	}
	defer client.Disconnect(context.Background())
}

Config Fields

  • URI: MongoDB connection string (required)
  • Database: default database name (required)
  • ConnectTimeout: optional connect timeout
  • PingTimeout: optional ping timeout
  • MaxPoolSize: optional driver max pool size (0 keeps defaults)

Documentation

Overview

Package mon_go provides a small, easy-to-use MongoDB client wrapper.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a wrapper

func New

func New(cfg Config) (*Client, error)

New creates and verifies a MongoDB client

func NewFromEnv

func NewFromEnv() (*Client, error)

NewFromEnv builds a client from env vars env vars: - MONGO_URI: connection string - MONGO_DB: default database name

func (*Client) Collection

func (c *Client) Collection(name string) *mongo.Collection

Collection returns a collection from the default database

func (*Client) DB

func (c *Client) DB() *mongo.Database

DB returns the default database

func (*Client) Disconnect

func (c *Client) Disconnect(ctx context.Context) error

Disconnect closes the client

func (*Client) Raw

func (c *Client) Raw() *mongo.Client

Raw returns the underlying mongo.Client

type Config

type Config struct {
	// connection string
	URI string
	// default database name
	Database string
	// connectTimeout bounds the initial connection attempt
	ConnectTimeout time.Duration
	// pingTimeout bounds the initial ping call
	PingTimeout time.Duration
	// maxPoolSize sets the driver's max pool size, use 0 to keep driver defaults
	MaxPoolSize uint64
}

Config holds the configuration for the MongoDB client

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL