mgo

package
v1.4.0 Latest Latest
Warning

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

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

Documentation

Overview

Package mgo provides MongoDB database connection and operation wrapper.

Index

Constants

View Source
const (
	MaxLen int = 5000 // batch size for bulk insert
)

Variables

This section is empty.

Functions

func Call

func Call(fn func(pool.Src) error) result.VoidResult

Call executes fn with a pooled MongoDB connection.

func Close

func Close()

Close shuts down the connection pool.

func CollectionNames

func CollectionNames(dbname string) result.Result[[]string]

CollectionNames returns collection names for the given database.

func DatabaseNames

func DatabaseNames() result.Result[[]string]

DatabaseNames returns all database names.

func Error

func Error() error

Error returns the last MongoDB connection error.

func Len

func Len() int

Len returns the current resource count.

func Mgo

func Mgo(resultPtr interface{}, operate string, option map[string]interface{}) result.Result[any]

Mgo is the unified entry for CRUD operations. resultPtr types: count=*int, list=*map[string][]string, find=*map[string]interface{}, insert=*[]string (may be nil to skip IDs), update/remove=nil.

func Refresh

func Refresh()

Refresh re-establishes MongoDB connection.

Types

type Count

type Count struct {
	Database   string                 // database name
	Collection string                 // collection name
	Query      map[string]interface{} // query filter
}

Count returns the number of documents matching the query.

func (*Count) Exec

func (cnt *Count) Exec(resultPtr interface{}) (r result.Result[any])

type Find

type Find struct {
	Database   string                 // database name
	Collection string                 // collection name
	Query      map[string]interface{} // query filter
	Sort       []string               // sort fields, e.g. Sort("firstname", "-lastname") for asc firstname, desc lastname
	Skip       int                    // skip first n documents
	Limit      int                    // return at most n documents
	Select     interface{}            // projection, e.g. {"name":1} to return only name
}

Find performs a conditional query on the specified collection.

func (*Find) Exec

func (f *Find) Exec(resultPtr interface{}) (r result.Result[any])

type Insert

type Insert struct {
	Database   string                   // database name
	Collection string                   // collection name
	Docs       []map[string]interface{} // documents to insert
}

Insert inserts new documents into the collection.

func (*Insert) Exec

func (i *Insert) Exec(resultPtr interface{}) (r result.Result[any])

type List

type List struct {
	Dbs []string // list of database names to query (empty = all)
}

List returns a map of database names to their collection names.

func (*List) Exec

func (l *List) Exec(resultPtr interface{}) (r result.Result[any])

type MgoSrc

type MgoSrc struct {
	*mgo.Session
}

MgoSrc wraps MongoDB session for connection pool.

func (*MgoSrc) Close

func (ms *MgoSrc) Close()

Close closes the session when removed from the pool.

func (*MgoSrc) Reset

func (*MgoSrc) Reset()

Reset is called when the resource is returned to the pool.

func (*MgoSrc) Usable

func (ms *MgoSrc) Usable() bool

Usable reports whether the MongoDB session is usable.

type Operator

type Operator interface {
	Exec(resultPtr interface{}) result.Result[any]
}

Operator defines the interface for CRUD operations.

type Remove

type Remove struct {
	Database   string                 // database name
	Collection string                 // collection name
	Selector   map[string]interface{} // document selector
}

Remove deletes documents matching the selector.

func (*Remove) Exec

func (r *Remove) Exec(_ interface{}) (res result.Result[any])

type Update

type Update struct {
	Database   string                 // database name
	Collection string                 // collection name
	Selector   map[string]interface{} // document selector
	Change     map[string]interface{} // update document
}

Update updates the first document matching the selector.

func (*Update) Exec

func (u *Update) Exec(_ interface{}) (r result.Result[any])

type UpdateAll

type UpdateAll struct {
	Database   string                 // database name
	Collection string                 // collection name
	Selector   map[string]interface{} // document selector
	Change     map[string]interface{} // update document
}

UpdateAll updates all documents matching the selector.

func (*UpdateAll) Exec

func (ua *UpdateAll) Exec(resultPtr interface{}) (r result.Result[any])

type Upsert

type Upsert struct {
	Database   string                 // database name
	Collection string                 // collection name
	Selector   map[string]interface{} // document selector
	Change     map[string]interface{} // update document
}

Upsert updates the first matching document, or inserts if none match.

func (*Upsert) Exec

func (us *Upsert) Exec(resultPtr interface{}) (r result.Result[any])

Jump to

Keyboard shortcuts

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