Documentation ¶
Index ¶
- func Create(ctx context.Context, collection *mongo.Collection, data interface{}) (primitive.ObjectID, error)
- func CreateFilterFromArrayValue(arrayField string, value interface{}) bson.M
- func CreateFilterFromArrayValuesMatchAll(arrayField string, values []interface{}) bson.M
- func CreateFilterFromArrayValuesMatchIn(arrayField string, values []interface{}) bson.M
- func CreateFilterFromQueryParams(queryParams map[string][]string, matchBehavior MatchBehavior, ...) bson.M
- func DecodeCursor(ctx context.Context, cursor *mongo.Cursor, receiver interface{}) ([]interface{}, error)
- func DeleteByID(ctx context.Context, collection *mongo.Collection, IDKey string, ...) error
- func FindAll(ctx context.Context, collection *mongo.Collection, receiver interface{}, ...) ([]interface{}, error)
- func FindByID(ctx context.Context, collection *mongo.Collection, receiver interface{}, ...) error
- func Search(ctx context.Context, collection *mongo.Collection, receiver interface{}, ...) (*structcup.SearchResponse, error)
- func UpdateByID(ctx context.Context, collection *mongo.Collection, data interface{}, ...) error
- type MatchBehavior
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Create ¶ added in v1.9.5
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 CreateFilterFromArrayValue ¶ added in v1.9.4
CreateFilterFromArrayValue 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 CreateFilterFromArrayValuesMatchAll ¶ added in v1.9.4
CreateFilterFromArrayValuesMatchAll 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 CreateFilterFromArrayValuesMatchIn ¶ added in v1.9.4
CreateFilterFromArrayValuesMatchIn 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 CreateFilterFromQueryParams ¶
func CreateFilterFromQueryParams(queryParams map[string][]string, matchBehavior MatchBehavior, excludedFields ...string) bson.M
CreateFilterFromQueryParams creates a BSON filter from the given query parameters.
This function is typically used to convert query parameters from a web request into a filter that can be used by a MongoDB query.
func DecodeCursor ¶ added in v1.9.19
func DecodeCursor(ctx context.Context, cursor *mongo.Cursor, receiver interface{}) ([]interface{}, error)
DecodeCursor decodes the documents in a MongoDB cursor into a slice.
A slice of decoded documents, or an error if one occurred.
func DeleteByID ¶ added in v1.9.5
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 FindAll ¶ added in v1.9.5
func FindAll(ctx context.Context, collection *mongo.Collection, receiver interface{}, queryParams map[string][]string, sortByKey string, sortDirection typecup.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 FindByID ¶ added in v1.9.5
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.
func Search ¶ added in v1.9.5
func Search(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.
func UpdateByID ¶ added in v1.9.5
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.
Types ¶
type MatchBehavior ¶ added in v1.9.4
type MatchBehavior string
MatchBehavior defines different behaviors for matching MongoDB documents.
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 typecup.SortDirection = 1 // Descending represents the descending sort direction. Descending typecup.SortDirection = -1 )