mgo

package
v1.12.5 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: MIT Imports: 10 Imported by: 0

README

mgo

mgo is a library wrapped on the official library mongo-go-driver, with added features paging queries.


Example of use

    import "github.com/go-dev-frame/sponge/pkg/mgo"

    // dsn document: https://www.mongodb.com/docs/manual/reference/connection-string/

    // case 1: specify options in dsn
    db, err := mgo.Init("mongodb://root:123456@192.168.3.37:27017/account?socketTimeoutMS=30000&maxPoolSize=100&minPoolSize=1&maxConnIdleTimeMS=300000")

    // case 2: specify options in code
    db, err := mgo.Init("mongodb://root:123456@192.168.3.37:27017/account",
        mgo.WithOption().SetMaxPoolSize(100),
        mgo.WithOption().SetMinPoolSize(1),
        mgo.WithOption().SetMaxConnIdleTime(5*time.Minute),
        mgo.WithOption().SetSocketTimeout(30*time.Second),
    )

    // close mongodb
    defer mgo.Close(db)

Documentation

Overview

Package mgo is a library wrapped on go.mongodb.org/mongo-driver/mongo, with added features paging queries, etc.

Index

Constants

View Source
const (
	// DBDriverName mongodb driver
	DBDriverName = "mongodb"
)

Variables

View Source
var ErrNoDocuments = mongo.ErrNoDocuments

Functions

func Close

func Close(db *mongo.Database) error

Close mongodb

func ConvertToObjectIDs

func ConvertToObjectIDs(ids []string) []primitive.ObjectID

ConvertToObjectIDs convert ids to objectIDs

func EmbedDeletedAt

func EmbedDeletedAt(update bson.M) bson.M

EmbedDeletedAt embed deleted_at datetime column

func EmbedUpdatedAt

func EmbedUpdatedAt(update bson.M) bson.M

EmbedUpdatedAt embed updated_at datetime column

func ExcludeDeleted

func ExcludeDeleted(filter bson.M) bson.M

ExcludeDeleted exclude soft deleted records

func Init

func Init(dsn string, opts ...*options.ClientOptions) (*mongo.Database, error)

Init connecting to mongo

func Init2

func Init2(uri string, dbName string, opts ...*options.ClientOptions) (*mongo.Database, error)

Init2 connecting to mongo using uri

func NewCustomLogger

func NewCustomLogger(l *zap.Logger, isDebugLevel bool) *options.LoggerOptions

NewCustomLogger create a custom logger for mongodb, debug level is used by default. example: WithOption().SetLoggerOptions(NewCustomLogger(logger.Get(), true))

func WithOption

func WithOption() *options.ClientOptions

WithOption set option for mongodb

Types

type Database

type Database = mongo.Database

type Model

type Model struct {
	ID        primitive.ObjectID `bson:"_id" json:"id"`
	CreatedAt time.Time          `bson:"created_at" json:"createdAt"`
	UpdatedAt time.Time          `bson:"updated_at" json:"updatedAt"`
	DeletedAt *time.Time         `bson:"deleted_at,omitempty" json:"deletedAt,omitempty"`
}

Model embedded structs, add `bson: ",inline"` when defining table structs

func (*Model) SetModelValue

func (p *Model) SetModelValue()

SetModelValue set model fields

Directories

Path Synopsis
Package query is a library of custom condition queries, support for complex conditional paging queries.
Package query is a library of custom condition queries, support for complex conditional paging queries.

Jump to

Keyboard shortcuts

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