mon

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: MIT Imports: 22 Imported by: 32

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = mongo.ErrNoDocuments

ErrNotFound is an alias of mongo.ErrNoDocuments

Functions

func DisableInfoLog added in v1.6.0

func DisableInfoLog()

DisableInfoLog disables info logging of mongo commands, but keeps slow logs.

func DisableLog added in v1.6.0

func DisableLog()

DisableLog disables logging of mongo commands, includes info and slow logs.

func FormatAddr

func FormatAddr(hosts []string) string

FormatAddr formats mongo hosts to a string.

func Inject

func Inject(key string, client *mongo.Client)

Inject injects a *mongo.Client into the client manager. Typically, this is used to inject a *mongo.Client for test purpose.

func SetSlowThreshold

func SetSlowThreshold(threshold time.Duration)

SetSlowThreshold sets the slow threshold.

Types

type BulkInserter

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

A BulkInserter is used to insert bulk of mongo records.

func NewBulkInserter

func NewBulkInserter(coll Collection, interval ...time.Duration) (*BulkInserter, error)

NewBulkInserter returns a BulkInserter.

func (*BulkInserter) Flush

func (bi *BulkInserter) Flush()

Flush flushes the inserter, writes all pending records.

func (*BulkInserter) Insert

func (bi *BulkInserter) Insert(doc any)

Insert inserts doc.

func (*BulkInserter) SetResultHandler

func (bi *BulkInserter) SetResultHandler(handler ResultHandler)

SetResultHandler sets the result handler.

type ClosableClient

type ClosableClient struct {
	*mongo.Client
}

ClosableClient wraps *mongo.Client and provides a Close method.

func (*ClosableClient) Close

func (cs *ClosableClient) Close() error

Close disconnects the underlying *mongo.Client.

type Collection

type Collection interface {
	// Aggregate executes an aggregation pipeline.
	Aggregate(ctx context.Context, pipeline any, opts ...*mopt.AggregateOptions) (
		*mongo.Cursor, error)
	// BulkWrite performs a bulk write operation.
	BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*mopt.BulkWriteOptions) (
		*mongo.BulkWriteResult, error)
	// Clone creates a copy of this collection with the same settings.
	Clone(opts ...*mopt.CollectionOptions) (*mongo.Collection, error)
	// CountDocuments returns the number of documents in the collection that match the filter.
	CountDocuments(ctx context.Context, filter any, opts ...*mopt.CountOptions) (int64, error)
	// Database returns the database that this collection is a part of.
	Database() *mongo.Database
	// DeleteMany deletes documents from the collection that match the filter.
	DeleteMany(ctx context.Context, filter any, opts ...*mopt.DeleteOptions) (
		*mongo.DeleteResult, error)
	// DeleteOne deletes at most one document from the collection that matches the filter.
	DeleteOne(ctx context.Context, filter any, opts ...*mopt.DeleteOptions) (
		*mongo.DeleteResult, error)
	// Distinct returns a list of distinct values for the given key across the collection.
	Distinct(ctx context.Context, fieldName string, filter any,
		opts ...*mopt.DistinctOptions) ([]any, error)
	// Drop drops this collection from database.
	Drop(ctx context.Context) error
	// EstimatedDocumentCount returns an estimate of the count of documents in a collection
	// using collection metadata.
	EstimatedDocumentCount(ctx context.Context, opts ...*mopt.EstimatedDocumentCountOptions) (int64, error)
	// Find finds the documents matching the provided filter.
	Find(ctx context.Context, filter any, opts ...*mopt.FindOptions) (*mongo.Cursor, error)
	// FindOne returns up to one document that matches the provided filter.
	FindOne(ctx context.Context, filter any, opts ...*mopt.FindOneOptions) (
		*mongo.SingleResult, error)
	// FindOneAndDelete returns at most one document that matches the filter. If the filter
	// matches multiple documents, only the first document is deleted.
	FindOneAndDelete(ctx context.Context, filter any, opts ...*mopt.FindOneAndDeleteOptions) (
		*mongo.SingleResult, error)
	// FindOneAndReplace returns at most one document that matches the filter. If the filter
	// matches multiple documents, FindOneAndReplace returns the first document in the
	// collection that matches the filter.
	FindOneAndReplace(ctx context.Context, filter, replacement any,
		opts ...*mopt.FindOneAndReplaceOptions) (*mongo.SingleResult, error)
	// FindOneAndUpdate returns at most one document that matches the filter. If the filter
	// matches multiple documents, FindOneAndUpdate returns the first document in the
	// collection that matches the filter.
	FindOneAndUpdate(ctx context.Context, filter, update any,
		opts ...*mopt.FindOneAndUpdateOptions) (*mongo.SingleResult, error)
	// Indexes returns the index view for this collection.
	Indexes() mongo.IndexView
	// InsertMany inserts the provided documents.
	InsertMany(ctx context.Context, documents []any, opts ...*mopt.InsertManyOptions) (
		*mongo.InsertManyResult, error)
	// InsertOne inserts the provided document.
	InsertOne(ctx context.Context, document any, opts ...*mopt.InsertOneOptions) (
		*mongo.InsertOneResult, error)
	// ReplaceOne replaces at most one document that matches the filter.
	ReplaceOne(ctx context.Context, filter, replacement any,
		opts ...*mopt.ReplaceOptions) (*mongo.UpdateResult, error)
	// UpdateByID updates a single document matching the provided filter.
	UpdateByID(ctx context.Context, id, update any,
		opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
	// UpdateMany updates the provided documents.
	UpdateMany(ctx context.Context, filter, update any,
		opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
	// UpdateOne updates a single document matching the provided filter.
	UpdateOne(ctx context.Context, filter, update any,
		opts ...*mopt.UpdateOptions) (*mongo.UpdateResult, error)
	// Watch returns a change stream cursor used to receive notifications of changes to the collection.
	Watch(ctx context.Context, pipeline any, opts ...*mopt.ChangeStreamOptions) (
		*mongo.ChangeStream, error)
}

Collection defines a MongoDB collection.

type Model

type Model struct {
	Collection
	// contains filtered or unexported fields
}

Model is a mongodb store model that represents a collection.

func MustNewModel

func MustNewModel(uri, db, collection string, opts ...Option) *Model

MustNewModel returns a Model, exits on errors.

func NewModel

func NewModel(uri, db, collection string, opts ...Option) (*Model, error)

NewModel returns a Model.

func (*Model) Aggregate

func (m *Model) Aggregate(ctx context.Context, v, pipeline any, opts ...*mopt.AggregateOptions) error

Aggregate executes an aggregation pipeline.

func (*Model) DeleteMany

func (m *Model) DeleteMany(ctx context.Context, filter any, opts ...*mopt.DeleteOptions) (int64, error)

DeleteMany deletes documents that match the filter.

func (*Model) DeleteOne

func (m *Model) DeleteOne(ctx context.Context, filter any, opts ...*mopt.DeleteOptions) (int64, error)

DeleteOne deletes the first document that matches the filter.

func (*Model) Find

func (m *Model) Find(ctx context.Context, v, filter any, opts ...*mopt.FindOptions) error

Find finds documents that match the filter.

func (*Model) FindOne

func (m *Model) FindOne(ctx context.Context, v, filter any, opts ...*mopt.FindOneOptions) error

FindOne finds the first document that matches the filter.

func (*Model) FindOneAndDelete

func (m *Model) FindOneAndDelete(ctx context.Context, v, filter any,
	opts ...*mopt.FindOneAndDeleteOptions) error

FindOneAndDelete finds a single document and deletes it.

func (*Model) FindOneAndReplace

func (m *Model) FindOneAndReplace(ctx context.Context, v, filter, replacement any,
	opts ...*mopt.FindOneAndReplaceOptions) error

FindOneAndReplace finds a single document and replaces it.

func (*Model) FindOneAndUpdate

func (m *Model) FindOneAndUpdate(ctx context.Context, v, filter, update any,
	opts ...*mopt.FindOneAndUpdateOptions) error

FindOneAndUpdate finds a single document and updates it.

func (*Model) StartSession

func (m *Model) StartSession(opts ...*mopt.SessionOptions) (sess mongo.Session, err error)

StartSession starts a new session.

type Option

type Option func(opts *options)

Option defines the method to customize a mongo model.

func WithTimeout added in v1.4.4

func WithTimeout(timeout time.Duration) Option

WithTimeout set the mon client operation timeout.

func WithTypeCodec added in v1.6.3

func WithTypeCodec(typeCodecs ...TypeCodec) Option

WithTypeCodec registers TypeCodecs to convert custom types.

type ResultHandler

type ResultHandler func(*mongo.InsertManyResult, error)

ResultHandler is a handler that used to handle results.

type TypeCodec added in v1.6.3

type TypeCodec struct {
	ValueType reflect.Type
	Encoder   bsoncodec.ValueEncoder
	Decoder   bsoncodec.ValueDecoder
}

TypeCodec is a struct that stores specific type Encoder/Decoder.

Jump to

Keyboard shortcuts

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