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 ¶
- type MongoLoadBalancer
- type MongoManager
- func (m *MongoManager) Close(ctx context.Context) error
- func (m *MongoManager) GetCollection(dbName, collName string, readOnly bool) (*mongo.Collection, error)
- func (m *MongoManager) GetDatabase(name string, readOnly bool) (*mongo.Database, error)
- func (m *MongoManager) Health(ctx context.Context) error
- func (m *MongoManager) Master() *mongo.Client
- func (m *MongoManager) Slave() (*mongo.Client, error)
- func (m *MongoManager) WithTransaction(ctx context.Context, fn func(mongo.SessionContext) error, ...) error
- type MongoRandomBalancer
- type MongoRoundRobinBalancer
- type MongoWeightBalancer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MongoLoadBalancer ¶
type MongoManager ¶
type MongoManager struct {
// contains filtered or unexported fields
}
func NewMongoManager ¶
func NewMongoManager(conf *config.MongoDB) (*MongoManager, error)
func (*MongoManager) GetCollection ¶
func (m *MongoManager) GetCollection(dbName, collName string, readOnly bool) (*mongo.Collection, error)
func (*MongoManager) GetDatabase ¶
func (*MongoManager) Master ¶
func (m *MongoManager) Master() *mongo.Client
func (*MongoManager) WithTransaction ¶
func (m *MongoManager) WithTransaction(ctx context.Context, fn func(mongo.SessionContext) error, opts ...*options.TransactionOptions) error
type MongoRandomBalancer ¶
type MongoRandomBalancer struct{}
type MongoRoundRobinBalancer ¶
type MongoRoundRobinBalancer struct {
// contains filtered or unexported fields
}
func NewMongoRoundRobinBalancer ¶
func NewMongoRoundRobinBalancer() *MongoRoundRobinBalancer
type MongoWeightBalancer ¶
type MongoWeightBalancer struct {
// contains filtered or unexported fields
}
func NewMongoWeightBalancer ¶
func NewMongoWeightBalancer(nodes []*config.MongoNode) *MongoWeightBalancer
Click to show internal directories.
Click to hide internal directories.