上下文对象
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设置它们。