长风秋雁
我刚刚遇到这个问题。因为mongo.Cursor有一个内部字段保存[]byte-- Current,为了完全模拟,您需要包装mongo.Cursor. 以下是我为此创建的类型:type MongoCollection interface { Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (MongoCursor, error) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) MongoDecoder Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (MongoCursor, error)}type MongoDecoder interface { DecodeBytes() (bson.Raw, error) Decode(val interface{}) error Err() error}type MongoCursor interface { Decode(val interface{}) error Err() error Next(ctx context.Context) bool Close(ctx context.Context) error ID() int64 Current() bson.Raw}type mongoCursor struct { mongo.Cursor}func (m *mongoCursor) Current() bson.Raw { return m.Cursor.Current}不幸的是,这将是一个移动的目标。MongoCollection随着时间的推移,我将不得不向界面添加新功能。