Context Object
The OpContext struct is used to pass operation context information. It contains the following fields:
type OpContext struct {
Col *mongo.Collection `opt:"-"` // The collection object
Fields []*field.Filed // Metadata of the struct fields bound to the Collection's generic type
Doc any // Document object (e.g., *T or []*T)
Filter any // Query filter
Updates any // Document updates
Pipeline any // Aggregation pipeline
MongoOptions any // MongoDB operation options
ModelHook any // Model hook
ReflectValue reflect.Value // Reflection value of the Doc (used to dynamically modify the document in plugins)
StartTime time.Time // Operation start time
Result any // Result of the MongoDB collection operation
}Field Descriptions:
Col: The*mongo.Collectionobject.Fields: A slice of[]*field.Filedobjects that store metadata of the structure fields bound to theCollectionobject’s generic type.Doc: The document object, representing the structure bound to theCollectionobject’s generic type (e.g.,*Tor[]*T).Filter: The query filter.Updates: The updates to be applied to the document.Pipeline: The aggregation pipeline.MongoOptions: MongoDB operation options.ModelHook: The model hook.ReflectValue: The reflection value ofDoc, used for dynamically modifying the document object inside plugins.StartTime: The start time of the operation.Result: The result of the MongoDB collection operation.
Different Operation Types and Their Context Fields
Depending on the operation type, the values of OpContext fields may differ. For example:
When
opTypeisoperation.OpTypeBeforeInsertoroperation.OpTypeAfterInsert:opCtx.Docmay be non-nil, and its type could be*structor[]*struct.opCtx.ReflectValuewill not be nil.
When
opTypeisoperation.OpTypeBeforeUpdateoroperation.OpTypeAfterUpdate:opCtx.FilterandopCtx.Updatesmay be non-nil.
When
opTypeisoperation.OpTypeBeforeDeleteoroperation.OpTypeAfterDelete:opCtx.Filtermay be non-nil.
When
opTypeisoperation.OpTypeBeforeUpsertoroperation.OpTypeAfterUpsert:opCtx.FilterandopCtx.Updatesmay be non-nil.
When
opTypeisoperation.OpTypeBeforeFindoroperation.OpTypeAfterFind:opCtx.Filtermay be non-nil.- If the type is
operation.OpTypeAfterFind,opCtx.Docmay be non-nil. opCtx.Resultmay be non-nil.
When
opTypeisoperation.OpTypeBeforeAnyoroperation.OpTypeAfterAny:- All fields in
OpContextcan be referenced according to the descriptions above.
- All fields in
Regardless of the operation type, the
Col,Fields, andStartTimefields ofOpContextwill never be nil.Regardless of the operation type,
MongoOptionsandResultmay be non-nil.Whether
OpContext.MongoOptionsandModelHookare nil depends on whether the user has set them viaCreator,Updater,Deleter,Finder, orAggregatorduring the MongoDB operation.