mongojuice

package
v1.17.4 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 12 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"

	Array            AtlasDataType = "array"
	Boolean          AtlasDataType = "boolean"
	Date             AtlasDataType = "date"
	DateFacet        AtlasDataType = "dateFacet"
	Double           AtlasDataType = "number"
	DoubleFacet      AtlasDataType = "numberFacet"
	DoubleDeprecated AtlasDataType = "knnVector" // Deprecated
	GeoJSON          AtlasDataType = "geo"
	Int32            AtlasDataType = "number"
	Int32Facet       AtlasDataType = "numberFacet"
	Int64            AtlasDataType = "number"
	Int64Facet       AtlasDataType = "numberFacet"
	Object           AtlasDataType = "document"
	ObjectArray      AtlasDataType = "embeddedDocument" // For array of objects
	ObjectID         AtlasDataType = "objectId"
	String           AtlasDataType = "string"
	StringFacet      AtlasDataType = "stringFacet"
	Autocomplete     AtlasDataType = "autocomplete"
	Token            AtlasDataType = "token"

	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, prependStr, appendStr 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, prependStr, appendStr 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, prependStr, appendStr 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 FilterByFieldValueInArray added in v1.12.17

func FilterByFieldValueInArray(field string, values []interface{}) bson.M

FilterByFieldValueInArray creates a BSON filter that matches documents where the specified field contains one of the values provided in the array.

This function 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(convertValues bool, queryParams map[string][]string, matchBehavior MatchBehavior, prependStr, appendStr string, 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 FindAllForceString added in v1.15.11

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

FindAllForceString 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.

This function is similar to FindAll, but it does not convert the values in the query parameters to their respective types. Generated BSON filter will contain the values as strings.

func FindAllPaginated added in v1.11.0

func FindAllPaginated(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, pagination *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 *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 *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 *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 *TextSearch, pagination *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.

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 *TextSearch, pagination *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 *TextSearch, pagination *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 *TextSearch, pagination *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 StartSessionAndTransaction added in v1.14.0

func StartSessionAndTransaction(ctx context.Context, client *mongo.Client) (mongo.SessionContext, error)

StartSessionAndTransaction starts a session and a transaction on the given client.

This method should be called prior to performing any operations that require a transaction.

Do not forget to end the session after the transaction is completed using defer session.EndSession().

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 AtlasDataType added in v1.15.12

type AtlasDataType = string

AtlasDataType represents the data type recognized by MongoDB Atlas.

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      `json:"operation_type"`
	FullDocument  interface{} `json:"full_document"`
}

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 PaginationOpts added in v1.15.12

type PaginationOpts struct {
	PageNumber int
	PageSize   int
}

PaginationOpts represents the options for pagination.

It includes a page number and a page size.

type SortDirection added in v1.11.0

type SortDirection = int

SortDirection defines the direction of sorting for MongoDB queries.

type TextSearch added in v1.15.12

type TextSearch struct {
	IndexName string
	Query     string
	Fields    []string
}

TextSearch represents a text search operation.

It includes an index name, a query, and fields to search in.

Jump to

Keyboard shortcuts

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