Documentation ¶
Index ¶
- Constants
- 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 DeleteByID(ctx context.Context, collection *mongo.Collection, IDKey string, ...) error
- func FindAll(ctx context.Context, collection *mongo.Collection, ...) ([]interface{}, error)
- func FindByID(ctx context.Context, collection *mongo.Collection, receiver interface{}, ...) error
- func Search(ctx context.Context, collection *mongo.Collection, ...) (*structcup.SearchResponse, error)
- func UpdateByID(ctx context.Context, collection *mongo.Collection, data interface{}, ...) error
- type MatchBehavior
Constants ¶
const ( Ascending typecup.SortDirection = 1 Descending typecup.SortDirection = -1 )
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.
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 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, 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.
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.
func Search ¶ added in v1.9.5
func Search(ctx context.Context, collection *mongo.Collection, 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.
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.
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" )