| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 | package moimport "context"// Shortcut 是操作 Collection 的快捷方式, 没有自定义功能type Shortcut struct {	coll *Collection}func NewShortcut(coll *Collection) *Shortcut {	return &Shortcut{coll: coll}}// Aggregate 管道聚合func (s *Shortcut) Aggregate(pipeline any, opts ...*AggregateOptions) (*Cursor, error) {	ctx, cancel := getCtx()	defer cancel()	if pipeline == nil {		pipeline = Pipeline{}	}	return s.coll.Aggregate(ctx, pipeline, opts...)}// Find 查找文档, 使用 filter 作为条件func (s *Shortcut) Find(filter any, opts ...*FindOptions) (*Cursor, error) {	ctx, cancel := getCtx()	defer cancel()	if filter == nil {		filter = D{}	}	return s.coll.Find(ctx, filter, opts...)}// FindOne 查找一条文档. 错误详情见 SingleResultfunc (s *Shortcut) FindOne(filter any, opts ...*FindOneOptions) *SingleResult {	ctx, cancel := getCtx()	defer cancel()	if filter == nil {		filter = D{}	}	return s.coll.FindOne(ctx, filter, opts...)}// FindOneAndDelete 查找一条文档, 然后删除. 错误详情见 SingleResultfunc (s *Shortcut) FindOneAndDelete(filter any, opts ...*FindOneAndDeleteOptions) *SingleResult {	ctx, cancel := getCtx()	defer cancel()	if filter == nil {		filter = D{}	}	return s.coll.FindOneAndDelete(ctx, filter, opts...)}// FindOneAndUpdate 查找一条文档, 然后使用 update 更新. 错误详情见 SingleResult// Update 操作符 https://www.mongodb.com/docs/manual/reference/operator/update-field/func (s *Shortcut) FindOneAndUpdate(filter, update any, opts ...*FindOneAndUpdateOptions) *SingleResult {	ctx, cancel := getCtx()	defer cancel()	if filter == nil {		filter = D{}	}	return s.coll.FindOneAndUpdate(ctx, filter, update, opts...)}// FindOneAndReplace TODOfunc (s *Shortcut) FindOneAndReplace() {}// CountDocuments 合集文档数量, 使用 filter 作为条件. 当不需要查询条件时推荐使用 EstimatedDocumentCountfunc (s *Shortcut) CountDocuments(filter any, opts ...*CountOptions) (int64, error) {	ctx, cancel := getCtx()	defer cancel()	if filter == nil {		filter = D{}	}	return s.coll.CountDocuments(ctx, filter, opts...)}// EstimatedDocumentCount 返回合集内文档的数量. 此方法无法添加过滤条件, 当需要过滤条件时使用 CountDocumentsfunc (s *Shortcut) EstimatedDocumentCount(opts ...*EstimatedDocumentCountOptions) (int64, error) {	ctx, cancel := getCtx()	defer cancel()	return s.coll.EstimatedDocumentCount(ctx, opts...)}// Indexes 索引操作func (s *Shortcut) Indexes() IndexView {	return s.coll.Indexes()}// InsertOne 插入一条文档func (s *Shortcut) InsertOne(doc any, opts ...*InsertOneOptions) (*InsertOneResult, error) {	ctx, cancel := getCtx()	defer cancel()	return s.coll.InsertOne(ctx, doc, opts...)}// InsertMany 插入多条文档func (s *Shortcut) InsertMany(doc []any, opts ...*InsertManyOptions) (*InsertManyResult, error) {	ctx, cancel := getCtx()	defer cancel()	return s.coll.InsertMany(ctx, doc, opts...)}// DeleteOne 删除一条文档, 使用 filter 作为条件func (s *Shortcut) DeleteOne(filter any, opts ...*DeleteOptions) (*DeleteResult, error) {	ctx, cancel := getCtx()	defer cancel()	if filter == nil {		filter = D{}	}	return s.coll.DeleteOne(ctx, filter, opts...)}// DeleteMany 删除多条文档, 使用 filter 作为条件func (s *Shortcut) DeleteMany(filter any, opts ...*DeleteOptions) (*DeleteResult, error) {	ctx, cancel := getCtx()	defer cancel()	if filter == nil {		filter = D{}	}	return s.coll.DeleteMany(ctx, filter, opts...)}// UpdateOne 更新一条文档, 使用 filter 作为条件func (s *Shortcut) UpdateOne(filter, update any, opts ...*UpdateOptions) (*UpdateResult, error) {	ctx, cancel := getCtx()	defer cancel()	if filter == nil {		filter = D{}	}	return s.coll.UpdateOne(ctx, filter, update, opts...)}// UpdateMany 更新多条文档, 使用 filter 作为条件func (s *Shortcut) UpdateMany(filter, update any, opts ...*UpdateOptions) (*UpdateResult, error) {	ctx, cancel := getCtx()	defer cancel()	if filter == nil {		filter = D{}	}	return s.coll.UpdateMany(ctx, filter, update, opts...)}// UpdateByID 更新一条文档, 使用 ObjectID 作为条件func (s *Shortcut) UpdateByID(id, update any, opts ...*UpdateOptions) (*UpdateResult, error) {	ctx, cancel := getCtx()	defer cancel()	return s.coll.UpdateByID(ctx, id, update, opts...)}// Drop 删除合集func (s *Shortcut) Drop() error {	ctx, cancel := getCtx()	defer cancel()	return s.coll.Drop(ctx)}func getCtx() (context.Context, context.CancelFunc) {	return context.WithTimeout(context.Background(), DefaultTimout)}
 |