mongodb

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

README

MongoDB Driver for ncore/data

This package provides a MongoDB driver implementation for the ncore/data framework.

Installation

go get github.com/ncobase/ncore/data/mongodb

Usage

Import the driver in your application to register it automatically:

import (
    "context"
    "github.com/ncobase/ncore/data"
    "github.com/ncobase/ncore/data/config"
    _ "github.com/ncobase/ncore/data/mongodb" // Register MongoDB driver
)

func main() {
    // Get the MongoDB driver
    driver, err := data.GetDatabaseDriver("mongodb")
    if err != nil {
        panic(err)
    }

    // Configure MongoDB connection
    cfg := &config.MongoDB{
        Master: &config.MongoNode{
            URI: "mongodb://localhost:27017/mydb",
        },
        Slaves: []*config.MongoNode{
            {
                URI: "mongodb://slave1:27017/mydb",
                Weight: 2,
            },
        },
        Strategy: "round_robin",
        MaxRetry: 3,
    }

    // Connect to MongoDB
    ctx := context.Background()
    conn, err := driver.Connect(ctx, cfg)
    if err != nil {
        panic(err)
    }
    defer driver.Close(conn)

    // Use the connection (conn is *mongo.Client)
    client := conn.(*mongo.Client)
    db := client.Database("mydb")
    // ... perform operations
}

Configuration

The driver expects a *config.MongoDB configuration struct with the following fields:

Master (required)

The primary MongoDB node configuration:

Master: &config.MongoNode{
    URI:     "mongodb://localhost:27017/mydb",  // MongoDB connection URI
    Logging: true,                              // Enable logging (optional)
    Weight:  1,                                 // Weight for load balancing (optional)
}
Slaves (optional)

Slave nodes for read operations:

Slaves: []*config.MongoNode{
    {
        URI:    "mongodb://slave1:27017/mydb",
        Weight: 2,
    },
    {
        URI:    "mongodb://slave2:27017/mydb",
        Weight: 1,
    },
}
Strategy (optional)

Load balancing strategy for slave nodes:

  • round_robin (default): Distributes requests evenly across slaves
  • random: Randomly selects a slave for each request
  • weight: Distributes requests based on node weights
MaxRetry (optional)

Maximum number of retry attempts for failed operations.

MongoDB URI Format

The MongoDB URI follows the standard MongoDB connection string format:

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
mongodb+srv://[username:password@]host[/[defaultauthdb][?options]]
Examples

Local development:

mongodb://localhost:27017/mydb

With authentication:

mongodb://user:password@localhost:27017/mydb?authSource=admin

MongoDB Atlas (cloud):

mongodb+srv://user:password@cluster.mongodb.net/mydb

Replica set:

mongodb://host1:27017,host2:27017,host3:27017/mydb?replicaSet=myrs

Features

  • ✅ Implements data.DatabaseDriver interface
  • ✅ Returns *mongo.Client for full MongoDB functionality
  • ✅ Automatic connection verification (ping on connect)
  • ✅ Proper context handling for all operations
  • ✅ Support for MongoDB connection options via URI
  • ✅ Master/slave configuration support
  • ✅ Multiple load balancing strategies
  • ✅ Connection pooling (configured via URI options)

Driver Methods

Name() string

Returns the driver identifier: "mongodb"

Connect(ctx context.Context, cfg interface{}) (interface{}, error)

Establishes a MongoDB connection using the provided *config.MongoDB configuration. Returns a *mongo.Client on success.

Close(conn interface{}) error

Closes the MongoDB connection and releases resources.

Ping(ctx context.Context, conn interface{}) error

Verifies the MongoDB connection is alive and functional.

Error Handling

The driver provides detailed error messages for common issues:

  • Invalid configuration type
  • Missing master configuration
  • Empty URI
  • Connection failures
  • Ping failures
  • Invalid connection type

All errors are wrapped with context using fmt.Errorf and %w for proper error chain support.

Testing

Run the test suite:

cd data/mongodb
go test -v

See Also

Documentation

Overview

Package mongodb provides a MongoDB driver for ncore/data.

This driver uses mongo-driver (go.mongodb.org/mongo-driver) as the underlying client. It registers itself automatically when imported:

import _ "github.com/ncobase/ncore/data/mongodb"

The driver supports MongoDB connection configuration including master/slave setup, load balancing strategies (round-robin, random, weight), and transaction support.

Example usage:

driver, err := data.GetDatabaseDriver("mongodb")
if err != nil {
    log.Fatal(err)
}

cfg := &config.MongoDB{
    Master: &config.MongoNode{
        URI: "mongodb://localhost:27017/mydb",
    },
    Slaves: []*config.MongoNode{
        {URI: "mongodb://slave1:27017/mydb", Weight: 2},
    },
    Strategy: "round_robin",
}

conn, err := driver.Connect(ctx, cfg)
manager := conn.(*MongoManager)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MongoLoadBalancer

type MongoLoadBalancer interface {
	Next([]*mongo.Client) (*mongo.Client, error)
}

type MongoManager

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

func NewMongoManager

func NewMongoManager(conf *config.MongoDB) (*MongoManager, error)

func (*MongoManager) Close

func (m *MongoManager) Close(ctx context.Context) error

func (*MongoManager) GetCollection

func (m *MongoManager) GetCollection(dbName, collName string, readOnly bool) (*mongo.Collection, error)

func (*MongoManager) GetDatabase

func (m *MongoManager) GetDatabase(name string, readOnly bool) (*mongo.Database, error)

func (*MongoManager) Health

func (m *MongoManager) Health(ctx context.Context) error

func (*MongoManager) Master

func (m *MongoManager) Master() *mongo.Client

func (*MongoManager) Slave

func (m *MongoManager) Slave() (*mongo.Client, error)

func (*MongoManager) WithTransaction

func (m *MongoManager) WithTransaction(ctx context.Context, fn func(mongo.SessionContext) error, opts ...*options.TransactionOptions) error

type MongoRandomBalancer

type MongoRandomBalancer struct{}

func (*MongoRandomBalancer) Next

func (rb *MongoRandomBalancer) Next(slaves []*mongo.Client) (*mongo.Client, error)

type MongoRoundRobinBalancer

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

func NewMongoRoundRobinBalancer

func NewMongoRoundRobinBalancer() *MongoRoundRobinBalancer

func (*MongoRoundRobinBalancer) Next

func (rb *MongoRoundRobinBalancer) Next(slaves []*mongo.Client) (*mongo.Client, error)

type MongoWeightBalancer

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

func NewMongoWeightBalancer

func NewMongoWeightBalancer(nodes []*config.MongoNode) *MongoWeightBalancer

func (*MongoWeightBalancer) Next

func (wb *MongoWeightBalancer) Next(slaves []*mongo.Client) (*mongo.Client, error)

Jump to

Keyboard shortcuts

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