mongox

package
v1.10.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNotFoundError added in v1.10.0

func IsNotFoundError(err error) bool

IsNotFoundError 检查是否是数据不存在错误

func IsUniqueConstraintError added in v1.10.0

func IsUniqueConstraintError(err error) bool

IsUniqueConstraintError 检查是否是唯一索引冲突错误

Types

type AutoIDModel added in v1.10.0

type AutoIDModel interface {
	SetID(id int64)
	GetID() int64
}

AutoIDModel 定义需要自动生成 ID 的模型接口

type Coll

type Coll struct {
	*Mongo
	// contains filtered or unexported fields
}

func (*Coll) Where

func (m *Coll) Where()

type Collection

type Collection interface {
	Where()
}

type Filter

type Filter interface{}

type IDPool added in v1.10.0

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

IDPool ID池,用于高并发场景的性能优化

func NewIDPool added in v1.10.0

func NewIDPool(mongo *Mongo, collectionName string, poolSize int) *IDPool

NewIDPool 创建一个ID池 poolSize: 每次从数据库获取多少个ID(例如1000)

func NewIDPoolWithRange added in v1.10.0

func NewIDPoolWithRange(mongo *Mongo, collectionName string, minSize, maxSize int) *IDPool

NewIDPoolWithRange 创建带自定义范围的ID池

func (*IDPool) Close added in v1.10.0

func (p *IDPool) Close(ctx context.Context) error

Close 优雅关闭ID池 尝试将未使用的ID归还到数据库(可选,用于减少浪费) 注意:这个操作有风险,只在优雅关闭时调用,crash时无法执行

func (*IDPool) NextBatch added in v1.10.0

func (p *IDPool) NextBatch(ctx context.Context, count int) (startID int64, err error)

NextBatch 批量获取ID(线程安全)

func (*IDPool) NextID added in v1.10.0

func (p *IDPool) NextID(ctx context.Context) (int64, error)

NextID 获取下一个ID(线程安全)

func (*IDPool) Remaining added in v1.10.0

func (p *IDPool) Remaining() int64

Remaining 返回池中剩余的ID数量

func (*IDPool) Stats added in v1.10.0

func (p *IDPool) Stats() map[string]interface{}

Stats 返回ID池的统计信息

type MapStr

type MapStr map[string]interface{}

type Mongo

type Mongo struct {
	DBClient *mongo.Client
	Sess     mongo.Session
	// contains filtered or unexported fields
}

func NewMongo

func NewMongo(client *mongo.Client, dbName string) *Mongo

func (*Mongo) Collection

func (m *Mongo) Collection(collName string) *mongo.Collection

func (*Mongo) Collections

func (m *Mongo) Collections(collName string) Collection

func (*Mongo) Database

func (m *Mongo) Database() *mongo.Database

func (*Mongo) GetBatchIdGenerator added in v1.10.0

func (m *Mongo) GetBatchIdGenerator(collection string, count int) (startID int64, err error)

GetBatchIdGenerator 批量获取自增 ID,提高批量插入性能

func (*Mongo) GetIdGenerator

func (m *Mongo) GetIdGenerator(collection string) int64

func (*Mongo) InsertManyWithAutoID added in v1.10.0

func (m *Mongo) InsertManyWithAutoID(ctx context.Context, collectionName string, docs []AutoIDModel) (*mongo.InsertManyResult, error)

InsertManyWithAutoID 批量插入文档,自动生成 ID(性能优化版)

func (*Mongo) InsertOneWithAutoID added in v1.10.0

func (m *Mongo) InsertOneWithAutoID(ctx context.Context, collectionName string, doc AutoIDModel) (*mongo.InsertOneResult, error)

InsertOneWithAutoID 插入单个文档,自动生成 ID

type Querier

type Querier[T any] interface {
	FindOne(ctx context.Context) (*T, error)
	FindMany(ctx context.Context) ([]*T, error)
}

Jump to

Keyboard shortcuts

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