上下文对象
OpContext
结构体用于传递操作上下文信息,它包含以下字段:
type OpContext struct {
Col *mongo.Collection `opt:"-"`
Fields []*field.Filed
Doc any
// filter also can be used as query
Filter any
Updates any
Pipeline any
MongoOptions any
ModelHook any
ReflectValue reflect.Value
StartTime time.Time
// result of the collection operation
Result any
}
结构体字段说明:
Col
:*mongo.Collection
对象Fields
:[]*field.Filed
对象,存储的是Collection
对象所绑定泛型的结构体字段元数据。Doc
:文档对象,为Collection
对象所绑定泛型的结构体对象,例如*T
或[]*T
。Filter
:查询条件。Updates
:更新文档。Pipeline
:聚合管道。MongoOptions
:mongo
操作选项。ModelHook
:模型钩子。ReflectValue
:为Doc
的反射值,可用于在插件里动态修改文档对象。StartTime
:操作开始时间。Result
:mongo collection
操作的结果。
不同的操作类型,OpContext
的字段值可能不同,例如:
当
opType
为operation.OpTypeBeforeInsert
或operation.OpTypeAfterInsert
时:opCtx.Doc
的值可能不为nil
,其类型可能是*struct
或[]*struct
。opCtx.ReflectValue
的值不为nil
。
当
opType
为operation.OpTypeBeforeUpdate
或operation.OpTypeAfterUpdate
时:opCtx.Filter
和opCtx.Updates
的值可能不为nil
。
当
opType
为operation.OpTypeBeforeDelete
或operation.OpTypeAfterDelete
时:opCtx.Filter
的值可能不为nil
。
当
opType
为operation.OpTypeBeforeUpsert
或operation.OpTypeAfterUpsert
时:opCtx.Filter
和opCtx.Updates
的值可能不为nil
。
当
opType
为operation.OpTypeBeforeFind
或operation.OpTypeAfterFind
时:opCtx.Filter
的值可能不为nil
- 如果类型是后者,
opCtx.Doc
的值可能不为nil
。 opCtx.Result
的值可能不为nil
。
当
opType
为operation.OpTypeBeforeAny
或operation.OpTypeAfterAny
时,OpContext
的所有字段值参考上述说明。无论是哪种操作类型,
OpContext
的Col
和Fields
以及StartTime
的值都不为nil
。无论是哪种操作类型,
OpContext
的opCtx.MongoOptions
和opCtx.Result
的值可能不为nil
。OpContext
的MongoOptions
和ModelHook
是否为nil
,取决于在执行MongoDB
操作时,使用者是否通过Creator
、Updater
、Deleter
、Finder
或Aggregator
设置它们。