mongo

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCollectionNotConfigured = errors.New("mongo: collection not correct configured")
View Source
var ErrDatabaseNotConfigured = errors.New("mongo: database not correct configured")
View Source
var ErrDestIsNotPointer = errors.New("mongo: dest param is not a pointer")
View Source
var ErrDestIsNotStruct = errors.New("mongo: dest param is not a struct")
View Source
var ErrDocumentIsEmpty = errors.New("mongo: document param is empty")
View Source
var ErrDocumentIsNotPointer = errors.New("mongo: document param is not a pointer")
View Source
var ErrDocumentIsNotStruct = errors.New("mongo: document param is not a struct")
View Source
var ErrDocumentsIsEmpty = errors.New("mongo: documents param is empty")
View Source
var ErrNoDocuments = errors.New("mongo: no documents in result")
View Source
var ErrNoOpenSession = errors.New("mongo: no open session")
View Source
var ErrRefDocument = errors.New("mongo: ref document needs to be structure or slice of the struct")

Functions

This section is empty.

Types

type DeleteResult added in v1.0.2

type DeleteResult struct {
	// The number of documents deleted.
	DeletedCount int64
}

DeleteResult is the result type returned by DeleteOne and DeleteMany operations.

type Event added in v1.1.2

type Event struct {
	DocumentKey       documentKey         `bson:"documentKey"`
	NS                ns                  `bson:"ns"`
	OperationType     string              `bson:"operationType"`
	FullDocument      FullDocument        `bson:"fullDocument"`
	UpdateDescription updateDescription   `bson:"updateDescription"`
	ClusterTime       primitive.Timestamp `bson:"clusterTime"`
}

type EventContext added in v1.0.9

type EventContext struct {
	context.Context
	Event Event
}

type EventHandler added in v1.0.9

type EventHandler func(ctx *EventContext)

type FirstBatchIndex

type FirstBatchIndex struct {
	V    int         `bson:"v,omitempty"`
	Key  primitive.M `bson:"key,omitempty"`
	Name string      `bson:"name,omitempty"`
	Ns   string      `bson:"ns,omitempty"`
}

type FullDocument added in v1.1.2

type FullDocument bson.M

func (FullDocument) Decode added in v1.1.6

func (f FullDocument) Decode(dest any) error

Decode convert Event.FullDocument to struct

type IndexInput

type IndexInput struct {
	// Keys A document describing which keys should be used for the index. It cannot be nil. This must be an order-preserving
	// type such as bson.D. Map types such as bson.M are not valid. See https://www.mongodb.com/docs/manual/indexes/#indexes
	// for examples of valid documents.
	Keys any
	// Options The options to use to create the index.
	Options *option.Index
	// Ref Struct reference contained database and collection tag
	Ref any
}

IndexInput represents a new index to be created.

type IndexResult added in v1.0.2

type IndexResult struct {
	Id         any             `bson:"id,omitempty"`
	Ns         string          `bson:"ns,omitempty"`
	FirstBatch FirstBatchIndex `bson:"firstBatch,omitempty"`
}

type IndexSpecification added in v1.0.2

type IndexSpecification struct {
	// The index name.
	Name string
	// The namespace for the index. This is a string in the format "databaseName.collectionName".
	Namespace string
	// The keys specification document for the index.
	KeysDocument bson.Raw
	// The index version.
	Version int32
	// The length of time, in seconds, for documents to remain in the collection. The default value is 0, which means
	// that documents will remain in the collection until they're explicitly deleted or the collection is dropped.
	ExpireAfterSeconds *int32
	// If true, the index will only reference documents that contain the fields specified in the index. The default is
	// false.
	Sparse *bool
	// If true, the collection will not accept insertion or update of documents where the index key value matches an
	// existing value in the index. The default is false.
	Unique *bool
	// The clustered index.
	Clustered *bool
}

IndexSpecification represents an index in a database. This type is returned by the IndexView.ListSpecifications function and is also used in the CollectionSpecification type.

type PageInput

type PageInput struct {
	// Page current page (default 0)
	Page int64
	// PageSize page size (required)
	PageSize int64
	// Ref struct reference contained database and collection configured
	Ref any
	// Sort value sort to result
	Sort any
}

type PageResult added in v1.0.2

type PageResult struct {
	Page          int64       `json:"page"`
	PageSize      int64       `json:"pageSize"`
	PageTotal     int64       `json:"pageTotal"`
	TotalElements int64       `json:"totalElements"`
	Content       pageContent `json:"content,omitempty"`
	LastQueryAt   time.Time   `json:"lastQueryAt,omitempty"`
}

func (PageResult) Decode added in v1.1.8

func (p PageResult) Decode(dest any) error

Decode parse pageResult to dest param

type Pipeline

type Pipeline []bson.D

Pipeline is a type that makes creating aggregation pipelines easier. It is a helper and is intended for serializing to BSON.

Example usage:

Pipeline{
	{{"$group", bson.D{{"_id", "$state"}, {"totalPop", bson.D{{"$sum", "$pop"}}}}}},
	{{"$match", bson.D{{"totalPop", bson.D{{"$gte", 10*1000*1000}}}}}},
}

type Sort

type Sort int
const (
	SortDesc Sort = -1
	SortAsc  Sort = 1
)

type Template

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

func NewTemplate

func NewTemplate(ctx context.Context, opts ...*options.ClientOptions) (*Template, error)

func (*Template) AbortTransaction

func (t *Template) AbortTransaction(ctx context.Context) error

AbortTransaction abort all transactions on session

func (*Template) Aggregate

func (t *Template) Aggregate(ctx context.Context, pipeline any, dest any, opts ...*option.Aggregate) error

Aggregate executes a find command, if successful it returns the corresponding documents in the collection in the dest parameter with return error nil. Otherwise, it returns corresponding error.

The pipeline parameter must be an array of documents, each representing an aggregation stage. The pipeline cannot be nil but can be empty. The stage documents must all be non-nil. For a pipeline of bson.D documents, the Pipeline type can be used. See https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/#db-collection-aggregate-stages for a list of valid stages in aggregations.

The dest parameter must be a pointer to the return expected by the operation, it is important to have the database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.Aggregate documentation.)

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/aggregate/.

func (*Template) CloseSession

func (t *Template) CloseSession(ctx context.Context, abort bool) error

CloseSession closes session and transaction, if param abort is false it will commit the changes, otherwise it will abort all transactions.

func (*Template) CommitTransaction

func (t *Template) CommitTransaction(ctx context.Context) error

CommitTransaction commit all transactions on session

func (*Template) CountDocuments

func (t *Template) CountDocuments(ctx context.Context, filter, ref any, opts ...*option.Count) (int64, error)

CountDocuments returns the number of documents in the collection. For a fast count of the documents in the collection, see the EstimatedDocumentCount method.

The filter parameter must be a document and can be used to select which documents contribute to the count. It cannot be nil. An empty document (e.g. bson.D{}) should be used to count all documents in the collection. This will result in a full collection scan.

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.Count documentation).

func (*Template) CreateManyIndex

func (t *Template) CreateManyIndex(ctx context.Context, inputs []IndexInput) ([]string, error)

CreateManyIndex executes a createIndexes command to create multiple indexes on the collection and returns the names of the new indexes.

For each IndexInput in the models parameter, the index name can be specified via the Options field. If a name is not given, it will be generated from the Keys document.

The opts parameter can be used to specify options for this operation (see the option.Index documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/createIndexes/.

func (*Template) CreateOneIndex

func (t *Template) CreateOneIndex(ctx context.Context, input IndexInput) (string, error)

CreateOneIndex executes a createIndexes command to create an index on the collection and returns the name of the new index. See the CreateManyIndex documentation for more information and an example. For this function's response, the name of the index is returned as a string, and if an error occurs, it is returned in the second return parameter

The opts parameter can be used to specify options for this operation (see the option.Index documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/createIndexes/.

func (*Template) DeleteMany

func (t *Template) DeleteMany(ctx context.Context, filter, ref any, opts ...*option.Delete) (*DeleteResult, error)

DeleteMany executes a delete command to delete documents from the collection.

The filter parameter must be a document containing query operators and can be used to select the documents to be deleted. It cannot be nil. An empty document (e.g. bson.D{}) should be used to delete all documents in the collection. If the filter does not match any documents, the operation will succeed and a DeleteResult with a DeletedCount of 0 will be returned.

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.Delete documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/delete/.

func (*Template) DeleteOne

func (t *Template) DeleteOne(ctx context.Context, filter, ref any, opts ...*option.Delete) (*DeleteResult, error)

DeleteOne executes a delete command to delete at most one document from the collection.

The filter parameter must be a document containing query operators and can be used to select the document to be deleted. It cannot be null. If the filter does not match any documents, the operation succeeds and a DeleteResult with a DeletedCount of 0 will be returned. If the filter matches multiple documents, one will be selected from the list matching set.

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.Delete documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/delete/.

func (*Template) DeleteOneById

func (t *Template) DeleteOneById(ctx context.Context, id, ref any, opts ...*option.Delete) (*DeleteResult, error)

DeleteOneById executes an update command to update the document whose _id value matches the provided ID in the collection. This is equivalent to running DeleteOne(ctx, bson.D{{"_id", id}}, ref, opts...).

The id parameter is the _id of the document to be updated. It cannot be nil. If the ID does not match any documents, the operation will succeed and an UpdateResult with a MatchedCount of 0 will be returned.

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.Delete documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/delete/.

func (*Template) Disconnect

func (t *Template) Disconnect(ctx context.Context) error

Disconnect closes the mongodb connection client with return error

func (*Template) Distinct

func (t *Template) Distinct(ctx context.Context, fieldName string, filter, dest, ref any, opts ...*option.Distinct) error

Distinct executes a distinct command to find the unique values for a specified field in the collection.

The fieldName parameter specifies the field name for which distinct values should be returned.

The filter parameter must be a document containing query operators and can be used to select which documents are considered. It cannot be nil. An empty document (e.g. bson.D{}) should be used to select all documents.

The dest parameter must be a pointer to the return expected by the operation.

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the options.DistinctOptions documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/distinct/.

func (*Template) DropAllIndexes

func (t *Template) DropAllIndexes(ctx context.Context, ref any, opts ...*option.DropIndex) error

DropAllIndexes executes a dropIndexes operation to drop all indexes on the collection. If the operation succeeds, this returns a BSON document in the form {nIndexesWas: <int32>}. The "nIndexesWas" field in the response contains the number of indexes that existed prior to the drop.

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for this operation (see the option.DropIndex documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/dropIndexes/.

func (*Template) DropCollection

func (t *Template) DropCollection(ctx context.Context, ref any) error

DropCollection drops the collection on the server. This method ignores "namespace not found" errors, so it is safe to drop a collection that does not exist on the server.

The ref parameter must be the collection structure with database and collection tags configured.

func (*Template) DropDatabase

func (t *Template) DropDatabase(ctx context.Context, ref any) error

DropDatabase drops the database on the server. This method ignores "namespace not found" errors, so it is safe to drop a database that does not exist on the server.

The ref parameter must be the collection structure with database and collection tags configured.

func (*Template) DropOneIndex

func (t *Template) DropOneIndex(ctx context.Context, name string, ref any, opts ...*option.DropIndex) error

DropOneIndex executes a dropIndexes operation to drop an index on the collection. If the operation succeeds, this returns a BSON document in the form {nIndexesWas: <int32>}. The "nIndexesWas" field in the response contains the number of indexes that existed prior to the drop.

The name parameter should be the name of the index to drop. If the name is "*", ErrMultipleIndexDrop will be returned without running the command because doing so would drop all indexes.

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for this operation (see the option.DropIndex documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/dropIndexes/.

func (*Template) EstimatedDocumentCount

func (t *Template) EstimatedDocumentCount(ctx context.Context, ref any, opts ...*option.EstimatedDocumentCount) (int64,
	error)

EstimatedDocumentCount executes a count command and returns an estimate of the number of documents in the collection using collection metadata.

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.EstimatedDocumentCount documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/count/.

func (*Template) Exists

func (t *Template) Exists(ctx context.Context, filter, ref any, opts ...*option.Exists) (bool, error)

Exists executes the count command, if the quantity is greater than 0 with a limit of 1, true is returned, otherwise false is returned.

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.Exists documentation).

func (*Template) ExistsById

func (t *Template) ExistsById(ctx context.Context, id, ref any, opts ...*option.Exists) (bool, error)

ExistsById executes a count command whose _id value matches the ID given in the collection. This is equivalent to running Exists(ctx, bson.D{{"_id", id}}, dest, opts...).

The ref parameter must be the collection structure with database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.Exists documentation).

func (*Template) Find

func (t *Template) Find(ctx context.Context, filter, dest any, opts ...*option.Find) error

Find executes a find command, if successful it returns the corresponding documents in the collection in the dest parameter with return error nil. Otherwise, it returns corresponding error.

The filter parameter must be a document containing query operators and can be used to select which documents are included in the result. If the filter does not match any document, the dest parameter will not be modified.

The dest parameter must be a pointer to the return expected by the operation, it is important to have the database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.Find documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.

func (*Template) FindAll

func (t *Template) FindAll(ctx context.Context, dest any, opts ...*option.Find) error

FindAll execute a search command. This is equivalent to running Find(ctx, bson.D{}, dest, opts...).

The dest parameter must be a pointer to the return expected by the operation, it is important to have the database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.Find documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.

func (*Template) FindOne

func (t *Template) FindOne(ctx context.Context, filter, dest any, opts ...*option.FindOne) error

FindOne executes a find command, if successful it returns the corresponding documents in the collection in the dest parameter with return error nil. Otherwise, it returns corresponding error.

The id parameter must be a document containing query operators and can be used to select the document to be returned. It cannot be null. If the id does not match any document, the error ErrNoDocuments will be returned.

The dest parameter must be a pointer to the return expected by the operation, it is important to have the database and collection tags configured.

The opts parameter can be used to specify options for this operation (see the option.FindOne documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.

func (*Template) FindOneAndDelete

func (t *Template) FindOneAndDelete(ctx context.Context, filter, dest any, opts ...*option.FindOneAndDelete) error

FindOneAndDelete executes a findAndModify command to delete at most one document from the collection. and returns the document as it appeared before deletion in the dest parameter.

The filter parameter must be a document containing query operators and can be used to select the document to be deleted. It cannot be null. If the filter does not match any documents, an error set to ErrNoDocuments will be returned. If the filter matches multiple documents, one will be selected from the matching set.

The dest parameter must be a pointer to the return expected by the operation, it is important to have the database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.FindOneAndDelete documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.

func (*Template) FindOneAndDeleteById added in v1.2.1

func (t *Template) FindOneAndDeleteById(ctx context.Context, id, dest any, opts ...*option.FindOneAndDelete) error

FindOneAndDeleteById executes a findAndModify command whose _id value matches the ID given in the collection. This is equivalent to running FindOneAndDelete(ctx, bson.D{{"_id", id}}, dest, opts...)

The id parameter is the _id of the document to be replaced. It cannot be null. If the filter does not match any documents, an error set to ErrNoDocuments will be returned. If the filter matches multiple documents, one will be selected from the matching set.

The dest parameter must be a pointer to the return expected by the operation, it is important to have the database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.FindOneAndDelete documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.

func (*Template) FindOneAndReplace

func (t *Template) FindOneAndReplace(ctx context.Context, filter, replacement, dest any, opts ...*option.FindOneAndReplace) error

FindOneAndReplace executes a findAndModify command to replace at most one document in the collection and returns the document as it appeared before replacement in the dest parameter.

The filter parameter must be a document containing query operators and can be used to select the document to be replaced. It cannot be null. If the filter does not match any documents, an error set to ErrNoDocuments will be returned. If the filter matches multiple documents, one will be selected from the matching set.

The replacement parameter must be a document that will be used to replace the selected document. It cannot be nil and cannot contain any update operators (https://www.mongodb.com/docs/manual/reference/operator/update/).

The dest parameter must be a pointer to the return expected by the operation, it is important to have the database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.FindOneAndReplace documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.

func (*Template) FindOneAndReplaceById added in v1.2.1

func (t *Template) FindOneAndReplaceById(ctx context.Context, id, replacement, dest any, opts ...*option.FindOneAndReplace) error

FindOneAndReplaceById executes a findAndModify command whose _id value matches the ID given in the collection. This is equivalent to running FindOneAndReplace(ctx, bson.D{{"_id", id}}, dest, opts...).

The id parameter is the _id of the document to be replaced. It cannot be null. If the filter does not match any documents, an error set to ErrNoDocuments will be returned. If the filter matches multiple documents, one will be selected from the matching set.

The replacement parameter must be a document that will be used to replace the selected document. It cannot be nil and cannot contain any update operators (https://www.mongodb.com/docs/manual/reference/operator/update/).

The dest parameter must be a pointer to the return expected by the operation, it is important to have the database and collection tags configured.

The opts parameter can be used to specify options for the operation (see the option.FindOneAndReplace documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.

func (*Template) FindOneAndUpdate

func (t *Template) FindOneAndUpdate(ctx context.Context, filter, update, dest any, opts ...*option.FindOneAndUpdate) error

FindOneAndUpdate executes a findAndModify command to update at most one document in the collection and returns the document as it appeared before updating.

The filter parameter must be a document containing query operators and can be used to select the document to be updated. It cannot be null. If the filter does not match any documents, an error set to ErrNoDocuments will be returned. If the filter matches multiple documents, one will be selected from the matching set.

The update parameter must be a document containing update operators (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be made to the selected document. It cannot be nil or empty.

The opts parameter can be used to specify options for the operation (see the options.FindOneAndUpdateOptions documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.

func (*Template) FindOneAndUpdateById added in v1.2.1

func (t *Template) FindOneAndUpdateById(ctx context.Context, id, update, dest any, opts ...*option.FindOneAndUpdate) error

FindOneAndUpdateById executes a findAndModify command whose _id value matches the ID given in the collection. This is equivalent to running FindOneAndUpdate(ctx, bson.D{{"_id", id}}, dest, opts...).

The id parameter is the _id of the document to be updated. It cannot be null. If the filter does not match any documents, an error set to ErrNoDocuments will be returned. If the filter matches multiple documents, one will be selected from the matching set.

The update parameter must be a document containing update operators (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be made to the selected document. It cannot be nil or empty.

The opts parameter can be used to specify options for the operation (see the options.FindOneAndUpdateOptions documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/findAndModify/.

func (*Template) FindOneById

func (t *Template) FindOneById(ctx context.Context, id, dest any, opts ...*option.FindOneById) error

FindOneById executes a search command whose _id value matches the ID given in the collection. This is equivalent to running FindOne(ctx, bson.D{{"_id", id}}, dest, opts...).

The id parameter must be a document containing query operators and can be used to select the document to be returned. It cannot be null. If the id does not match any document, the error ErrNoDocuments will be returned.

The dest parameter must be a pointer to the return expected by the operation, it is important to have the database and collection tags configured.

The opts parameter can be used to specify options for this operation (see the option.FindOneById documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.

func (*Template) FindPageable

func (t *Template) FindPageable(ctx context.Context, filter any, input PageInput, opts ...*option.FindPageable) (
	*PageResult, error)

FindPageable executes a find command, if successful, returns the paginated documents in the corresponding PageResult structure in the collection on the target parameter with null return error. Otherwise, it will return the corresponding error.

The filter parameter must be a document containing query operators and can be used to select which documents are included in the result. It cannot be nil. If the filter does not match any document, the return structure columns will be empty.

The opts parameter can be used to specify options for the operation (see the option.FindPageable documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/find/.

func (*Template) GetClient added in v1.0.7

func (t *Template) GetClient() mongo.Client

GetClient get mongo client used on template

func (*Template) InsertMany

func (t *Template) InsertMany(ctx context.Context, documents any, opts ...*option.InsertMany) error

InsertMany executes an insert command to insert multiple documents into the collection. If recording errors occur during operation you can configure automatic rollback, see the option.insertMany documentation.

The documents parameter must be a structure pointer slice to be inserted. The slice cannot be null or empty. All elements must be non-zero. For any document that does not have the _id field when transformed into BSON, the field value will be automatically generated and added to the slice pointer.

The opts parameter can be used to specify options for the operation (see the option.Change documentation.)

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/insert/.

func (*Template) InsertOne

func (t *Template) InsertOne(ctx context.Context, document any, opts ...*option.InsertOne) error

InsertOne executes an insert command to insert a single document into the collection.

The document parameter must be a structure pointer to be inserted, it must be non-zero. If it does not have the _id field when transformed into BSON, the field value is automatically generated and will be added to the document pointer provided.

The opts parameter can be used to specify options for the operation (see the option.Change documentation.)

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/insert/.

func (*Template) ListIndexSpecifications

func (t *Template) ListIndexSpecifications(ctx context.Context, ref any, opts ...*option.ListIndexes) (
	[]IndexSpecification, error)

ListIndexSpecifications executes a List command and returns a slice of returned IndexSpecifications.

The ref parameter must be the collection structure with database and collection tags configured.

func (*Template) ListIndexes

func (t *Template) ListIndexes(ctx context.Context, ref any, opts ...*option.ListIndexes) ([]IndexResult, error)

ListIndexes executes a listIndexes command and returns a cursor over the indexes in the collection.

The opts parameter can be used to specify options for this operation (see the option.ListIndexes documentation).

The ref parameter must be the collection structure with database and collection tags configured.

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/listIndexes/.

func (*Template) ReplaceOne

func (t *Template) ReplaceOne(ctx context.Context, filter any, update, ref any, opts ...*option.Replace) (*UpdateResult,
	error)

ReplaceOne executes an update command to replace at most one document in the collection.

The filter parameter must be a document containing query operators and can be used to select the document to be replaced. It cannot be nil. If the filter does not match any documents, the operation will succeed and an UpdateResult with a MatchedCount of 0 will be returned. If the filter matches multiple documents, one will be selected from the matched set and MatchedCount will equal 1.

The ref parameter must be the collection structure with database and collection tags configured.

The replacement parameter must be a document that will be used to replace the selected document. It cannot be nil and cannot contain any update operators (https://www.mongodb.com/docs/manual/reference/operator/update/).

The opts parameter can be used to specify options for the operation (see the option.Replace documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.

func (*Template) ReplaceOneById

func (t *Template) ReplaceOneById(ctx context.Context, id, replacement, ref any, opts ...*option.Replace) (*UpdateResult,
	error)

ReplaceOneById executes an update command to update the document whose _id value matches the provided ID in the collection. This is equivalent to running ReplaceOne(ctx, bson.D{{"_id", id}}, replacement, ref, opts...).

The id parameter is the _id of the document to be updated. It cannot be nil. If the ID does not match any documents, the operation will succeed and an UpdateResult with a MatchedCount of 0 will be returned.

The opts parameter can be used to specify options for the operation (see the option.Replace documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.

func (*Template) SetGlobalOption added in v1.2.1

func (t *Template) SetGlobalOption(opt *option.Global)

SetGlobalOption sets value for the mongo global options.

func (*Template) SimpleDisconnect added in v1.0.4

func (t *Template) SimpleDisconnect(ctx context.Context)

SimpleDisconnect closes the mongodb connection client without return error

func (*Template) StartSession added in v1.0.3

func (t *Template) StartSession(ctx context.Context) error

StartSession creates a new session and a new transaction and stores it in the template itself for the next operations.

func (*Template) UpdateMany

func (t *Template) UpdateMany(ctx context.Context, filter any, update, ref any, opts ...*option.Update) (*UpdateResult,
	error)

UpdateMany executes an update command to update documents in the collection.

The filter parameter must be a document containing query operators and can be used to select the documents to be updated. It cannot be nil. If the filter does not match any documents, the operation will succeed and an UpdateResult with a MatchedCount of 0 will be returned.

The ref parameter must be the collection structure with database and collection tags configured.

The update parameter must be a document containing update operators (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be made to the selected documents. It cannot be nil or empty.

The opts parameter can be used to specify options for the operation (see the option.Update documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.

func (*Template) UpdateOne

func (t *Template) UpdateOne(ctx context.Context, filter any, update, ref any, opts ...*option.Update) (*UpdateResult,
	error)

UpdateOne executes an update command to update at most one document in the collection.

The filter parameter must be a document containing query operators and can be used to select the document to be updated. It cannot be nil. If the filter does not match any documents, the operation will succeed and an UpdateResult with a MatchedCount of 0 will be returned. If the filter matches multiple documents, one will be selected from the matched set and MatchedCount will equal 1.

The ref parameter must be the collection structure with database and collection tags configured.

The update parameter must be a document containing update operators (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be made to the selected document. It cannot be nil or empty.

The opts parameter can be used to specify options for the operation (see the option.Update documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.

func (*Template) UpdateOneById

func (t *Template) UpdateOneById(ctx context.Context, id, update, ref any, opts ...*option.Update) (*UpdateResult, error)

UpdateOneById executes an update command to update the document whose _id value matches the provided ID in the collection. This is equivalent to running UpdateOne(ctx, bson.D{{"_id", id}}, update, ref, opts...).

The id parameter is the _id of the document to be updated. It cannot be nil. If the ID does not match any documents, the operation will succeed and an UpdateResult with a MatchedCount of 0 will be returned.

The ref parameter must be the collection structure with database and collection tags configured.

The update parameter must be a document containing update operators (https://www.mongodb.com/docs/manual/reference/operator/update/) and can be used to specify the modifications to be made to the selected document. It cannot be nil or empty.

The opts parameter can be used to specify options for the operation (see the option.Update documentation).

For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/update/.

func (*Template) Watch

func (t *Template) Watch(ctx context.Context, pipeline any, opts ...*option.Watch) (*mongo.ChangeStream, error)

Watch returns a change stream for all changes on the deployment. See https://www.mongodb.com/docs/manual/changeStreams/ for more information about change streams.

The client must be configured with read concern majority or no read concern for a change stream to be created successfully.

The pipeline parameter must be an array of documents, each representing a pipeline stage. The pipeline cannot be nil or empty. The stage documents must all be non-nil. See https://www.mongodb.com/docs/manual/changeStreams/ for a list of pipeline stages that can be used with change streams. For a pipeline of bson.D documents, the mongo.Pipeline{} type can be used.

The opts parameter can be used to specify options for change stream creation (see the option.Watch documentation).

func (*Template) WatchWithHandler added in v1.0.8

func (t *Template) WatchWithHandler(ctx context.Context, pipeline any, handler EventHandler,
	opts ...*option.WatchWithHandler) error

WatchWithHandler is a function that facilitates the reading of watch events, it triggers the Watch function and when an event occurs it reads this event transforming all the data obtained by mongoDB into a Context, right after this conversion, we call the handler parameter passing the context with all the information, so you can process it in the way you see fit.

The opts parameter can be used to specify options for change stream creation (see the option.WatchWithHandler documentation).

type UpdateResult added in v1.0.2

type UpdateResult struct {
	// The number of documents matched by the filter.
	MatchedCount int64
	// The number of documents modified by the operation.
	ModifiedCount int64
	// The number of documents upserted by the operation.
	UpsertedCount int64
	// The _id field of the upserted document, or nil if no upsert was done.
	UpsertedID any
}

UpdateResult is the result type returned from UpdateOne, UpdateMany, and ReplaceOne operations.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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