Documentation ¶
Index ¶
- func AggregateOption() *options.AggregateOptions
- func Array(args ...any) primitive.A
- func BatchUpdate[T any](condition any, updates any, opts ...MongoOption) (*mongo.UpdateResult, error)
- func BatchUpdateCtx[T any](ctx context.Context, condition any, updates any, opts ...MongoOption) (*mongo.UpdateResult, error)
- func Count[T any](filter any, opts ...MongoOption) (int64, error)
- func CountCtx[T any](ctx context.Context, filter any, opts ...MongoOption) (int64, error)
- func Delete[T any](v *T, opts ...MongoOption) (*mongo.DeleteResult, error)
- func DeleteCtx[T any](ctx context.Context, v *T, opts ...MongoOption) (*mongo.DeleteResult, error)
- func Doc(args ...any) primitive.D
- func Find[T any](filter any, sorts any, skip int64, limit int64, opts ...MongoOption) ([]T, error)
- func FindCtx[T any](ctx context.Context, filter any, sorts any, skip int64, limit int64, ...) ([]T, error)
- func FindOne[T any](filter any, sorts any, opts ...MongoOption) (*T, error)
- func FindOneCtx[T any](ctx context.Context, filter any, sorts any, opts ...MongoOption) (*T, error)
- func FindOption(sort any, skip int64, limit int64) *options.FindOptions
- func In(k string, v ...any) primitive.M
- func Increment[T any](condition any, data any, opts ...MongoOption) (*mongo.UpdateResult, error)
- func IncrementCtx[T any](ctx context.Context, condition any, data any, opts ...MongoOption) (*mongo.UpdateResult, error)
- func Insert[T any](v *T, opts ...MongoOption) (*mongo.InsertOneResult, error)
- func InsertCtx[T any](ctx context.Context, v *T, opts ...MongoOption) (*mongo.InsertOneResult, error)
- func IsValidObjectId(id *primitive.ObjectID) bool
- func Map(args ...any) primitive.M
- func Maps(args ...any) []primitive.M
- func Match(v any) primitive.M
- func MongoOperationCtx() (context.Context, context.CancelFunc)
- func ParseObjectID(id string) *primitive.ObjectID
- func Patch[T any](condition any, data primitive.M, silent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
- func PatchCtx[T any](ctx context.Context, condition any, data primitive.M, silent bool, ...) (*mongo.UpdateResult, error)
- func Regex(pattern string, opt string) primitive.Regex
- func RegexFor(k string, pattern string, opt string) primitive.M
- func Set(v any) primitive.M
- func SetNested(k string, v any) primitive.M
- func TxOption() *options.TransactionOptions
- func Update[T any](v *T, silent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
- func UpdateCtx[T any](ctx context.Context, v *T, silent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
- type BaseModel
- func (*BaseModel) Cleanup()
- func (*BaseModel) Collection(db *mongo.Database) *mongo.Collection
- func (model *BaseModel) GetID() primitive.ObjectID
- func (*BaseModel) Index(db *mongo.Database) error
- func (*BaseModel) IsDeletable() bool
- func (*BaseModel) IsEditable() bool
- func (model *BaseModel) NewId()
- func (*BaseModel) OnDelete(ctx context.Context, opt ...MongoOption)
- func (*BaseModel) OnDeleted(ctx context.Context, opt ...MongoOption)
- func (*BaseModel) OnInsert(ctx context.Context, opt ...MongoOption)
- func (*BaseModel) OnInserted(ctx context.Context, opt ...MongoOption)
- func (*BaseModel) OnUpdate(ctx context.Context, opt ...MongoOption)
- func (*BaseModel) OnUpdated(old any, ctx context.Context, opt ...MongoOption)
- func (*BaseModel) Pipeline() MongoPipeline
- func (model *BaseModel) PrepareInsert()
- func (model *BaseModel) PrepareUpdate(ghost bool)
- func (*BaseModel) Seed(db *mongo.Database) error
- func (model *BaseModel) SetID(id primitive.ObjectID)
- func (*BaseModel) TypeName() string
- type EmptyModel
- func (*EmptyModel) Cleanup()
- func (*EmptyModel) Collection(db *mongo.Database) *mongo.Collection
- func (*EmptyModel) GetID() primitive.ObjectID
- func (*EmptyModel) Index(db *mongo.Database) error
- func (*EmptyModel) IsDeletable() bool
- func (*EmptyModel) IsEditable() bool
- func (*EmptyModel) NewId()
- func (*EmptyModel) OnDelete(ctx context.Context, opt ...MongoOption)
- func (*EmptyModel) OnDeleted(ctx context.Context, opt ...MongoOption)
- func (*EmptyModel) OnInsert(ctx context.Context, opt ...MongoOption)
- func (*EmptyModel) OnInserted(ctx context.Context, opt ...MongoOption)
- func (*EmptyModel) OnUpdate(ctx context.Context, opt ...MongoOption)
- func (*EmptyModel) OnUpdated(old any, ctx context.Context, opt ...MongoOption)
- func (*EmptyModel) Pipeline() MongoPipeline
- func (*EmptyModel) PrepareInsert()
- func (*EmptyModel) PrepareUpdate(ghost bool)
- func (*EmptyModel) Seed(db *mongo.Database) error
- func (*EmptyModel) SetID(id primitive.ObjectID)
- func (*EmptyModel) TypeName() string
- type IrCity
- func (ic *IrCity) Cleanup()
- func (*IrCity) Collection(db *mongo.Database) *mongo.Collection
- func (ic *IrCity) GetID() primitive.ObjectID
- func (ic *IrCity) Index(db *mongo.Database) error
- func (*IrCity) IsEditable() bool
- func (ic *IrCity) NewId()
- func (i *IrCity) Seed(db *mongo.Database) error
- func (ic *IrCity) SetID(id primitive.ObjectID)
- func (*IrCity) SinglePipeline() MongoPipeline
- func (*IrCity) TypeName() string
- type MetaCounter
- type MetaCounterResult
- type MetaSetter
- type MetaSetterResult
- type Model
- type MongoDoc
- type MongoOption
- type MongoPipeline
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AggregateOption ¶ added in v1.1.0
func AggregateOption() *options.AggregateOptions
AggregateOption generate aggregation options
func BatchUpdate ¶ added in v1.4.0
func BatchUpdate[T any](condition any, updates any, opts ...MongoOption) (*mongo.UpdateResult, error)
func BatchUpdateCtx ¶ added in v1.4.3
func BatchUpdateCtx[T any]( ctx context.Context, condition any, updates any, opts ...MongoOption, ) (*mongo.UpdateResult, error)
BatchUpdate update multiple records
@param ctx operation context @param condition update condition @param updates update value @opts operation option
func CountCtx ¶ added in v1.4.3
Count get records count
@param ctx operation context @param filter (ignored on nil) @opts operation option
func Delete ¶ added in v1.4.0
func Delete[T any](v *T, opts ...MongoOption) (*mongo.DeleteResult, error)
func DeleteCtx ¶ added in v1.4.3
func DeleteCtx[T any]( ctx context.Context, v *T, opts ...MongoOption, ) (*mongo.DeleteResult, error)
Delete delete record
@param ctx operation context @param v model @opts operation option
func Doc ¶ added in v1.1.6
Doc generate primitive.D from args
Args count must even Example: Doc("_id", 1, "name", "John")
func FindCtx ¶ added in v1.4.3
func FindCtx[T any]( ctx context.Context, filter any, sorts any, skip int64, limit int64, opts ...MongoOption, ) ([]T, error)
Find find records
@param ctx operation context @param filter (ignored on nil) @param sorts (ignored on nil) @param skip (ignored on 0) @param limit (ignored on 0) @opts operation option
func FindOne ¶ added in v1.4.0
func FindOne[T any](filter any, sorts any, opts ...MongoOption) (*T, error)
func FindOneCtx ¶ added in v1.4.3
func FindOneCtx[T any]( ctx context.Context, filter any, sorts any, opts ...MongoOption, ) (*T, error)
FindOne find one record
@param ctx operation context @param filter (ignored on nil) @param sorts (ignored on nil) @opts operation option
func FindOption ¶ added in v1.1.0
func FindOption(sort any, skip int64, limit int64) *options.FindOptions
FindOption generate find option with sorts params
func Increment ¶ added in v1.4.0
func Increment[T any](condition any, data any, opts ...MongoOption) (*mongo.UpdateResult, error)
func IncrementCtx ¶ added in v1.4.3
func IncrementCtx[T any]( ctx context.Context, condition any, data any, opts ...MongoOption, ) (*mongo.UpdateResult, error)
Increment increment numeric data pass negative value for decrement increment run on silent mode
@param ctx operation context @param condition update condition @param data update value @opts operation option
func Insert ¶ added in v1.4.0
func Insert[T any](v *T, opts ...MongoOption) (*mongo.InsertOneResult, error)
func InsertCtx ¶ added in v1.4.3
func InsertCtx[T any]( ctx context.Context, v *T, opts ...MongoOption, ) (*mongo.InsertOneResult, error)
Insert insert new record this function use FindOne to find old record
@param ctx operation context @param v model @opts operation option
func IsValidObjectId ¶
IsValidObjectId check if object id is valid and not zero
func MongoOperationCtx ¶ added in v1.4.0
func MongoOperationCtx() (context.Context, context.CancelFunc)
MongoOperationCtx create context for mongo db operations for 10 sec
func ParseObjectID ¶
ParseObjectID parse object id from string
func Patch ¶ added in v1.4.0
func Patch[T any](condition any, data primitive.M, silent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
func PatchCtx ¶ added in v1.4.3
func PatchCtx[T any]( ctx context.Context, condition any, data primitive.M, silent bool, opts ...MongoOption, ) (*mongo.UpdateResult, error)
Patch partial update multiple records using $set
@param ctx operation context @param condition update condition @param data update value @param silent disable update meta (updated_at) @opts operation option
func RegexFor ¶ added in v1.1.6
RegexFor generate map with regex parameter
{ "name": { pattern: "John.*", options: "i" } }
func TxOption ¶ added in v1.1.8
func TxOption() *options.TransactionOptions
TxOption generate transaction option with majority write and snapshot read
func Update ¶ added in v1.4.0
func Update[T any](v *T, silent bool, opts ...MongoOption) (*mongo.UpdateResult, error)
func UpdateCtx ¶ added in v1.4.3
func UpdateCtx[T any]( ctx context.Context, v *T, silent bool, opts ...MongoOption, ) (*mongo.UpdateResult, error)
Update update one record
@param ctx operation context @param v model @param silent disable update meta (updated_at) @opts operation option
Types ¶
type BaseModel ¶ added in v1.3.0
type BaseModel struct { ID primitive.ObjectID `bson:"_id,omitempty" json:"_id"` CreatedAt time.Time `bson:"created_at" json:"created_at"` UpdatedAt *time.Time `bson:"updated_at" json:"updated_at"` }
BaseModel implementation with id and timestamp
func (*BaseModel) Collection ¶ added in v1.3.0
func (*BaseModel) Collection(db *mongo.Database) *mongo.Collection
func (*BaseModel) IsDeletable ¶ added in v1.3.0
func (*BaseModel) IsEditable ¶ added in v1.3.0
func (*BaseModel) OnDelete ¶ added in v1.3.0
func (*BaseModel) OnDelete(ctx context.Context, opt ...MongoOption)
func (*BaseModel) OnDeleted ¶ added in v1.3.0
func (*BaseModel) OnDeleted(ctx context.Context, opt ...MongoOption)
func (*BaseModel) OnInsert ¶ added in v1.3.0
func (*BaseModel) OnInsert(ctx context.Context, opt ...MongoOption)
func (*BaseModel) OnInserted ¶ added in v1.3.0
func (*BaseModel) OnInserted(ctx context.Context, opt ...MongoOption)
func (*BaseModel) OnUpdate ¶ added in v1.3.0
func (*BaseModel) OnUpdate(ctx context.Context, opt ...MongoOption)
func (*BaseModel) OnUpdated ¶ added in v1.3.0
func (*BaseModel) OnUpdated(old any, ctx context.Context, opt ...MongoOption)
func (*BaseModel) Pipeline ¶ added in v1.3.0
func (*BaseModel) Pipeline() MongoPipeline
func (*BaseModel) PrepareInsert ¶ added in v1.3.0
func (model *BaseModel) PrepareInsert()
func (*BaseModel) PrepareUpdate ¶ added in v1.3.0
type EmptyModel ¶ added in v1.3.1
type EmptyModel struct{}
EmptyModel only implement model methods
func (*EmptyModel) Cleanup ¶ added in v1.3.1
func (*EmptyModel) Cleanup()
func (*EmptyModel) Collection ¶ added in v1.3.1
func (*EmptyModel) Collection(db *mongo.Database) *mongo.Collection
func (*EmptyModel) GetID ¶ added in v1.3.2
func (*EmptyModel) GetID() primitive.ObjectID
func (*EmptyModel) IsDeletable ¶ added in v1.3.1
func (*EmptyModel) IsDeletable() bool
func (*EmptyModel) IsEditable ¶ added in v1.3.1
func (*EmptyModel) IsEditable() bool
func (*EmptyModel) NewId ¶ added in v1.3.1
func (*EmptyModel) NewId()
func (*EmptyModel) OnDelete ¶ added in v1.3.1
func (*EmptyModel) OnDelete(ctx context.Context, opt ...MongoOption)
func (*EmptyModel) OnDeleted ¶ added in v1.3.1
func (*EmptyModel) OnDeleted(ctx context.Context, opt ...MongoOption)
func (*EmptyModel) OnInsert ¶ added in v1.3.1
func (*EmptyModel) OnInsert(ctx context.Context, opt ...MongoOption)
func (*EmptyModel) OnInserted ¶ added in v1.3.1
func (*EmptyModel) OnInserted(ctx context.Context, opt ...MongoOption)
func (*EmptyModel) OnUpdate ¶ added in v1.3.1
func (*EmptyModel) OnUpdate(ctx context.Context, opt ...MongoOption)
func (*EmptyModel) OnUpdated ¶ added in v1.3.1
func (*EmptyModel) OnUpdated(old any, ctx context.Context, opt ...MongoOption)
func (*EmptyModel) Pipeline ¶ added in v1.3.1
func (*EmptyModel) Pipeline() MongoPipeline
func (*EmptyModel) PrepareInsert ¶ added in v1.3.1
func (*EmptyModel) PrepareInsert()
func (*EmptyModel) PrepareUpdate ¶ added in v1.3.1
func (*EmptyModel) PrepareUpdate(ghost bool)
func (*EmptyModel) SetID ¶ added in v1.3.1
func (*EmptyModel) SetID(id primitive.ObjectID)
func (*EmptyModel) TypeName ¶ added in v1.3.6
func (*EmptyModel) TypeName() string
type IrCity ¶ added in v1.5.1
type IrCity struct { EmptyModel `bson:"inline"` ID primitive.ObjectID `bson:"_id" json:"_id"` Code uint `bson:"code" json:"code"` Name string `bson:"name" json:"name"` CountyId *primitive.ObjectID `bson:"county_id" json:"county_id"` ProvinceId *primitive.ObjectID `bson:"province_id" json:"province_id"` County *IrCity `bson:"county,omitempty" json:"county"` Province *IrCity `bson:"province,omitempty" json:"province"` }
IrCity only implement model methods
func (*IrCity) Collection ¶ added in v1.5.1
func (*IrCity) Collection(db *mongo.Database) *mongo.Collection
func (*IrCity) IsEditable ¶ added in v1.5.1
func (*IrCity) SinglePipeline ¶ added in v1.5.1
func (*IrCity) SinglePipeline() MongoPipeline
type MetaCounter ¶ added in v1.2.4
type MetaCounter interface { // Add new meta Add(_col, _meta string, id *primitive.ObjectID, amount int) MetaCounter // Build get combined meta with query Build() []MetaCounterResult }
func NewMetaCounter ¶ added in v1.2.0
func NewMetaCounter() MetaCounter
NewMetaCounter new mongo meta counter
type MetaCounterResult ¶ added in v1.2.4
type MetaSetter ¶ added in v1.2.4
type MetaSetter interface { // Add new meta Add(_col, _meta string, id *primitive.ObjectID, value any) MetaSetter // Build get combined meta with query Build() []MetaSetterResult }
func NewMetaSetter ¶ added in v1.2.4
func NewMetaSetter() MetaSetter
NewMetaSetter new mongo meta setter
type MetaSetterResult ¶ added in v1.2.4
type Model ¶ added in v1.1.0
type Model interface { // TypeName get type string TypeName() string // Collection get model collection Collection(db *mongo.Database) *mongo.Collection // Indexes create model indexes Index(db *mongo.Database) error // Seed run model seed Seed(db *mongo.Database) error // Pipeline get model pipeline Pipeline() MongoPipeline // NewId generate new id for model NewId() // SetID set model id SetID(id primitive.ObjectID) // ID get model id GetID() primitive.ObjectID // IsEditable check if document is editable // by default returns true on BaseModel IsEditable() bool // IsDeletable check if document is deletable // by default returns false on BaseModel IsDeletable() bool // Cleanup document before save // e.g set document field nil for ignore saving Cleanup() // PrepareInsert fill created_at before save PrepareInsert() // PrepareUpdate fill updated_at before save // in ghost mode updated_at field not changed PrepareUpdate(ghost bool) // OnInsert function to call before insert OnInsert(ctx context.Context, opt ...MongoOption) // OnUpdate function to call before update OnUpdate(ctx context.Context, opt ...MongoOption) // OnDelete function to call before delete OnDelete(ctx context.Context, opt ...MongoOption) // OnInserted function to call after insert OnInserted(ctx context.Context, opt ...MongoOption) // OnUpdated function to call after update OnUpdated(old any, ctx context.Context, opt ...MongoOption) // OnDeleted function to call after delete OnDeleted(ctx context.Context, opt ...MongoOption) }
model interface
type MongoDoc ¶
type MongoDoc interface { // Add add new element Add(k string, v any) MongoDoc // Doc add new element with nested doc value Doc(k string, cb func(d MongoDoc) MongoDoc) MongoDoc // Array add new element with array value Array(k string, v ...any) MongoDoc // DocArray add new array element with doc DocArray(k string, cb func(d MongoDoc) MongoDoc) MongoDoc // Nested add new nested element Nested(root string, k string, v any) MongoDoc // NestedDoc add new nested element with doc value NestedDoc(root string, k string, cb func(d MongoDoc) MongoDoc) MongoDoc // NestedArray add new nested element with array value NestedArray(root string, k string, v ...any) MongoDoc // NestedDocArray add new nested array element with doc NestedDocArray(root string, k string, cb func(d MongoDoc) MongoDoc) MongoDoc // Regex add new element with regex value Regex(k string, pattern string, opt string) MongoDoc // Map creates a map from the elements of the Doc Map() primitive.M // Build generate mongo doc Build() primitive.D }
MongoDoc mongo document (primitive.D) builder
type MongoOption ¶ added in v1.4.0
type MongoPipeline ¶
type MongoPipeline interface { // Add add new Doc Add(cb func(d MongoDoc) MongoDoc) MongoPipeline // Match add $match stage. skip nil input Match(filters any) MongoPipeline // In add $in stage In(key string, v any) MongoPipeline // Limit add $limit stage (ignore negative and zero value) Limit(limit int64) MongoPipeline // Skip add $skip stage (ignore negative and zero value) Skip(skip int64) MongoPipeline // Sort add $sort stage (ignore nil value) Sort(sorts any) MongoPipeline // Unwind add $unwind stage Unwind(path string, prevNullAndEmpty bool) MongoPipeline // Lookup add $lookup stage Lookup(from string, local string, foreign string, as string) MongoPipeline // Unwrap get first item of array and insert to doc using $addFields stage Unwrap(field string, as string) MongoPipeline // LoadRelation load related document using $lookup and $addField LoadRelation(from string, local string, foreign string, as string) MongoPipeline // Group add $group stage Group(cb func(d MongoDoc) MongoDoc) MongoPipeline // ReplaceRoot add $replaceRoot stage ReplaceRoot(v any) MongoPipeline // MergeRoot add $replaceRoot stage with $mergeObjects operator MergeRoot(fields ...any) MongoPipeline // UnProject generate $project stage to remove fields from result UnProject(fields ...string) MongoPipeline // Project add $project stage. skip nil input Project(projects any) MongoPipeline // Build generate mongo pipeline Build() mongo.Pipeline }
MongoPipeline mongo pipeline (mongo.Pipeline) builder