mongojuice

package
v1.11.19 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Description: This package provides a set of utility functions for working with MongoDB databases using the official MongoDB Go driver. The functions in this package are designed to be used in combination with the official MongoDB Go driver. The package provides functions for creating, reading, updating, and deleting documents in a MongoDB collection. It also provides functions for creating BSON filters from query parameters, and for performing text searches and paginated searches.

Index

Constants

View Source
const (
	// MatchAll matches documents where the field value contains all of the specified values.
	MatchAll MatchBehavior = "$all"
	// MatchIn matches documents where the field value is in the specified list of values.
	MatchIn MatchBehavior = "$in"
	// MatchEqual matches documents where the field value is equal to the specified value.
	MatchEqual MatchBehavior = "$eq"
	// Ascending represents the ascending sort direction.
	Ascending SortDirection = 1
	// Descending represents the descending sort direction.
	Descending SortDirection = -1
	// AndOperator represents the $and operator.
	AndOperator BSONOperator = "$and"
	// OrOperator represents the $or operator.
	OrOperator BSONOperator = "$or"
	// NorOperator represents the $nor operator.
	NorOperator BSONOperator = "$nor"

	MongoOperationTypeCreate                   MongoOperationType = "create"
	MongoOperationTypeCreateIndexes            MongoOperationType = "createIndexes"
	MongoOperationTypeDelete                   MongoOperationType = "delete"
	MongoOperationTypeDrop                     MongoOperationType = "drop"
	MongoOperationTypeDropDatabase             MongoOperationType = "dropDatabase"
	MongoOperationTypeDropIndexes              MongoOperationType = "dropIndexes"
	MongoOperationTypeInsert                   MongoOperationType = "insert"
	MongoOperationTypeInvalidate               MongoOperationType = "invalidate"
	MongoOperationTypeModify                   MongoOperationType = "modify"
	MongoOperationTypeRefineCollectionShardKey MongoOperationType = "refineCollectionShardKey"
	MongoOperationTypeRename                   MongoOperationType = "rename"
	MongoOperationTypeReplace                  MongoOperationType = "replace"
	MongoOperationTypeReshardCollection        MongoOperationType = "reshardCollection"
	MongoOperationTypeShardCollection          MongoOperationType = "shardCollection"
	MongoOperationTypeUpdate                   MongoOperationType = "update"

	MongoLookupFullDocument  MongoLookupTarget = "fullDocument"
	MongoLookupOperationType MongoLookupTarget = "operationType"
)

Variables

This section is empty.

Functions

func Create

func Create(ctx context.Context, collection *mongo.Collection, data interface{}) (primitive.ObjectID, error)

Create creates a new document in the given collection using the provided data.

The data parameter should be a struct or a pointer to a struct.

func DeleteByID

func DeleteByID(ctx context.Context, collection *mongo.Collection, IDKey string, IDValue primitive.ObjectID) error

DeleteByID deletes a document in the given collection by its ID.

func FilterByAllArrayValues added in v1.11.0

func FilterByAllArrayValues(arrayField string, values []interface{}) bson.M

FilterByAllArrayValues creates a BSON filter that matches documents where the specified values are contained in the specified array field.

This function uses the `$all` operator to match documents where the specified values are in the specified list of values.

func FilterByArrayValue added in v1.11.0

func FilterByArrayValue(arrayField string, value interface{}) bson.M

FilterByArrayValue creates a BSON filter that matches documents where the specified value is contained in the specified array field.

This function uses the `$elemMatch` operator to match documents where the specified value is equal to the specified value.

It uses the $eq operator to match documents where the field value is equal to the specified value.

func FilterByArrayValues added in v1.11.0

func FilterByArrayValues(arrayField string, values []interface{}) bson.M

FilterByArrayValues creates a BSON filter that matches documents where the specified values are contained in the specified array field.

This function uses the `$elemMatch` operator to match documents where the specified values are in the specified list of values.

It uses the $in operator to match documents where the field value is in the specified list of values.

func FilterFromQueryParams added in v1.11.0

func FilterFromQueryParams(queryParams map[string][]string, matchBehavior MatchBehavior, excludedFields ...string) (bson.M, error)

FilterFromQueryParams creates a BSON filter from the given query parameters.

This function is used to convert query parameters from a web request into a filter that can be used by a MongoDB query.

func FindAll

func FindAll(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, sortByKey string, sortDirection SortDirection) ([]interface{}, error)

FindAll finds all documents in the given collection, filters them based on the provided query parameters, and sorts them by the given key and direction.

The underlying type of each element in the slice of interface returned will correspond to the type of the receiver.

The receiver type should be the zero value of the desired type or a pointer to it.

func FindAllByArrayValue added in v1.11.5

func FindAllByArrayValue(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, arrayField string, value interface{}, sortByKey string, sortDirection SortDirection) ([]interface{}, error)

FindAllByArrayValue finds all documents in the given collection, filters them based on the provided array field and value, and sorts them by the given key and direction.

Results will contain only document where the specified value is contained in the specified array field.

In this method, queryParams it not mandatory, but it can be used to filter the results even more.

func FindAllPaginated added in v1.11.0

func FindAllPaginated(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, pagination *structcup.PaginationOpts, searchQueryKey, pageQueryKey, perPageQueryKey, sortByKey string, sortDirection SortDirection) (*structcup.SearchResponse, error)

FindAllPaginated finds all documents in the given collection, filters them based on the provided query parameters, and sorts them by the given key and direction.

The underlying type of each element in the slice of interface (Results field) returned will correspond to the type of the receiver.

The receiver type should be the zero value of the desired type or a pointer to it.

Documents will match all the query parameters, but if a query parameter has multiple values, documents will match any of the values within the slice.

func FindAllPaginatedByAllArrayValues added in v1.11.0

func FindAllPaginatedByAllArrayValues(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, arrayField string, values []interface{}, pagination *structcup.PaginationOpts, sortByKey string, sortDirection SortDirection) (*structcup.SearchResponse, error)

FindAllPaginatedByAllArrayValues finds all documents in the given collection, filters them based on the provided array field and values, and sorts them by the given key and direction.

Results will contain only document where all of the specified values are contained in the specified array field.

In this method, queryParams it not mandatory, but it can be used to filter the results even more.

func FindAllPaginatedByArrayValue added in v1.11.0

func FindAllPaginatedByArrayValue(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, arrayField string, value interface{}, pagination *structcup.PaginationOpts, sortByKey string, sortDirection SortDirection) (*structcup.SearchResponse, error)

FindAllPaginatedByArrayValue finds all documents in the given collection, filters them based on the provided array field and value, and sorts them by the given key and direction.

Results will contain only document where the specified value is contained in the specified array field.

In this method, queryParams it not mandatory, but it can be used to filter the results even more.

func FindAllPaginatedByArrayValues added in v1.11.0

func FindAllPaginatedByArrayValues(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, arrayField string, values []interface{}, pagination *structcup.PaginationOpts, sortByKey string, sortDirection SortDirection) (*structcup.SearchResponse, error)

FindAllPaginatedByArrayValues finds all documents in the given collection, filters them based on the provided array field and values, and sorts them by the given key and direction.

Results will contain only document where a minimum of one of the specified values is contained in the specified array field.

In this method, queryParams it not mandatory, but it can be used to filter the results even more.

func FindByID

func FindByID(ctx context.Context, collection *mongo.Collection, receiver interface{}, IDKey string, IDValue primitive.ObjectID) error

FindByID finds a document in the given collection by its ID and decodes it into the provided receiver.

The receiver type should be a pointer to an instance of the desired type to decode the document into.

func SearchText added in v1.11.0

func SearchText(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, textSearch *structcup.TextSearch, pagination *structcup.PaginationOpts, searchQueryKey, pageQueryKey, perPageQueryKey string) (*structcup.SearchResponse, error)

Search searches for documents in the given collection based on the provided query parameters, text search criteria, and pagination options.

The underlying type of each element in the slice of interface (Results field) returned will correspond to the type of the receiver.

The receiver type should be the zero value of the desired type or a pointer to it.

Omit passing textSearch to perform a simple FindAll paginated search instead of a text search.

func SearchTextByAllArrayValues added in v1.11.1

func SearchTextByAllArrayValues(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, arrayField string, values []interface{}, textSearch *structcup.TextSearch, pagination *structcup.PaginationOpts) (*structcup.SearchResponse, error)

SearchTextByAllArrayValues searches for documents in the given collection based on the provided array field and values, text search criteria, and pagination options.

The underlying type of each element in the slice of interface (Results field) returned will correspond to the type of the receiver.

The receiver type should be the zero value of the desired type or a pointer to it.

Results will contain only document where all of the specified values are contained in the specified array field.

In this method, queryParams it not mandatory, but it can be used to filter the results even more.

func SearchTextByArrayValue added in v1.11.1

func SearchTextByArrayValue(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, arrayField string, value interface{}, textSearch *structcup.TextSearch, pagination *structcup.PaginationOpts) (*structcup.SearchResponse, error)

SearchTextByArrayValues searches for documents in the given collection based on the provided array field and values, text search criteria, and pagination options.

The underlying type of each element in the slice of interface (Results field) returned will correspond to the type of the receiver.

The receiver type should be the zero value of the desired type or a pointer to it.

Results will contain only document where the specified value is contained in the specified array field.

In this method, queryParams it not mandatory, but it can be used to filter the results even more.

func SearchTextByArrayValues added in v1.11.1

func SearchTextByArrayValues(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, arrayField string, values []interface{}, textSearch *structcup.TextSearch, pagination *structcup.PaginationOpts) (*structcup.SearchResponse, error)

SearchTextByArrayValues searches for documents in the given collection based on the provided array field and values, text search criteria, and pagination options.

The underlying type of each element in the slice of interface (Results field) returned will correspond to the type of the receiver.

The receiver type should be the zero value of the desired type or a pointer to it.

Results will contain only document where a minimum of one of the specified values is contained in the specified array field.

In this method, queryParams it not mandatory, but it can be used to filter the results even more.

func UpdateByID

func UpdateByID(ctx context.Context, collection *mongo.Collection, data interface{}, IDKey string, IDValue primitive.ObjectID) error

UpdateByID updates a document in the given collection by its ID.

The data parameter should be a struct or a pointer to a struct.

func WatchCollection added in v1.11.3

func WatchCollection(ctx context.Context, collection *mongo.Collection, receiver interface{}, pipeline *mongo.Pipeline, opts *options.ChangeStreamOptions, out MongoOutCSChan) error

WatchCollection watches a collection for changes and sends the decoded responses out of the function.

The underlying type of each element passed to sendResponse will correspond to the type of the receiver.

The receiver type should be the zero value of the desired type or a pointer to it.

A short context should not be used with this function, as it may cause the function to return prematurely.

func WatchDatabase added in v1.11.3

func WatchDatabase(ctx context.Context, db *mongo.Database, receiver interface{}, pipeline *mongo.Pipeline, opts *options.ChangeStreamOptions, out MongoOutCSChan) error

WatchDatabase watches a database for changes and sends the decoded responses out of the function.

The underlying type of each element passed to sendResponse will correspond to the type of the receiver.

The receiver type should be the zero value of the desired type or a pointer to it.

A short context should not be used with this function, as it may cause the function to return prematurely.

Types

type BSONOperator added in v1.11.2

type BSONOperator = string

BSONOperator defines different BSON operators for MongoDB queries.

type ChangeEvent added in v1.11.10

type ChangeEvent struct {
	OperationType string
	FullDocument  interface{}
}

ChangeEvent represents a change event in a MongoDB change stream.

type MatchBehavior

type MatchBehavior = string

MatchBehavior defines different behaviors for matching MongoDB documents.

type MongoLookupTarget added in v1.11.3

type MongoLookupTarget = string

MongoLookupTarget defines the target field for the $lookup stage in a MongoDB aggregation pipeline for change streams.

type MongoOperationType added in v1.11.3

type MongoOperationType = string

MongoOperationType constants represent the operationType values for MongoDB change stream pipeline to be utilized with "operationType" field.

type MongoOutCSChan added in v1.11.7

type MongoOutCSChan = chan *ChangeEvent

MongoOutCSChan is a type alias for a channel that sends decoded MongoDB change stream responses out a function.

type SortDirection added in v1.11.0

type SortDirection = int

SortDirection defines the direction of sorting for MongoDB queries.

Jump to

Keyboard shortcuts

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