对比
MODM 封装了部分官方驱动的方法,未封装的方法可以通过 Collection()
或者 Clone()
获取 *mongo.Collection
调用
Mongo-go-driver | MODM |
---|---|
Clone | ✅ |
Name | ✅ |
Database | |
BulkWrite | |
InsertOne | ✅ |
InsertMany | ✅ |
DeleteOne | ✅ |
DeleteMany | ✅ |
UpdateByID | ✅ |
UpdateOne | ✅ |
UpdateMany | ✅ |
ReplaceOne | |
Aggregate | ✅ |
CountDocuments | Count |
EstimatedDocumentCount | EstimatedCount |
Distinct | ✅ |
Find | ✅ |
FindOne | ✅ |
FindOneAndDelete | ✅ |
FindOneAndReplace | |
FindOneAndUpdate | ✅ |
Watch | |
Indexes | |
Drop | |
Get | |
EnsureIndexes | |
EnsureIndexesByModel | |
Collection |
此外,还支持钩子、索引、事务等功能
参考资料
MODM:
go
type IRepo[T Document] interface {
Aggregate(ctx context.Context, pipeline interface{}, res interface{}, opts ...*options.AggregateOptions) error
Clone(opts ...*options.CollectionOptions) (*mongo.Collection, error)
Collection() *mongo.Collection
Count(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (deletedCount int64, err error)
DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (deletedCount int64, err error)
Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)
EnsureIndexes(ctx context.Context, uniques []string, indexes []string, indexModels ...mongo.IndexModel) error
EnsureIndexesByModel(ctx context.Context, model Indexes) error
EstimatedCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (docs []T, err error)
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (doc T, err error)
FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) (doc T, err error)
FindOneAndUpdate(ctx context.Context, filter interface{}, updateOrDoc interface{}, opts ...*options.FindOneAndUpdateOptions) (T, error)
Get(ctx context.Context, id interface{}, opts ...*options.FindOneOptions) (T, error)
InsertMany(ctx context.Context, docs []T, opts ...*options.InsertManyOptions) error
InsertOne(ctx context.Context, doc T, opts ...*options.InsertOneOptions) (T, error)
Name() string
UpdateByID(ctx context.Context, id interface{}, updateOrDoc interface{}, opts ...*options.UpdateOptions) (modifiedCount int64, err error)
UpdateMany(ctx context.Context, filter interface{}, updateOrDoc interface{}, opts ...*options.UpdateOptions) (modifiedCount int64, err error)
UpdateOne(ctx context.Context, filter interface{}, updateOrDoc interface{}, opts ...*options.UpdateOptions) (modifiedCount int64, err error)
}
mongo-go-driver:
go
type IMongoCollection interface {
Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)
BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
Clone(opts ...*options.CollectionOptions) (*mongo.Collection, error)
CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
Database() *mongo.Database
DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)
Drop(ctx context.Context) error
EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (cur *mongo.Cursor, err error)
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
FindOneAndReplace(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult
FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult
Indexes() mongo.IndexView
InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
Name() string
ReplaceOne(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
UpdateByID(ctx context.Context, id interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateMany(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)
}