github.com/aaabigfish/gopkg@v1.1.0/database/mongo/collection.go (about)

     1  package mongo
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"go.mongodb.org/mongo-driver/bson"
     8  	"go.mongodb.org/mongo-driver/mongo"
     9  	"go.mongodb.org/mongo-driver/mongo/options"
    10  )
    11  
    12  type BaseCollection interface {
    13  	SelectPage(ctx context.Context, filter interface{}, sort interface{}, skip, limit int64) (int64, []interface{}, error)
    14  	SelectList(ctx context.Context, filter interface{}, sort interface{}) ([]interface{}, error)
    15  	SelectListForExpect(ctx context.Context, filter interface{}, sort interface{}, expect interface{}, limit int64) (interface{}, error)
    16  	SelectOne(ctx context.Context, filter interface{}) (interface{}, error)
    17  	SelectCount(ctx context.Context, filter interface{}) (int64, error)
    18  	UpdateOne(ctx context.Context, filter, update interface{}) (int64, error)
    19  	UpsertOne(ctx context.Context, filter, update interface{}) (int64, error)
    20  	UpdateMany(ctx context.Context, filter, update interface{}) (int64, error)
    21  	Delete(ctx context.Context, filter interface{}) (int64, error)
    22  	InsetOne(ctx context.Context, model interface{}) (interface{}, error)
    23  	InsertMany(ctx context.Context, models []interface{}) ([]interface{}, error)
    24  	InsertManyIgnore(ctx context.Context, models []interface{}) ([]interface{}, error)
    25  	Aggregate(ctx context.Context, pipeline interface{}, result interface{}) error
    26  	CreateIndexes(ctx context.Context, indexes []mongo.IndexModel) error
    27  	GetCollection() *mongo.Collection
    28  }
    29  
    30  type BaseCollectionImpl struct {
    31  	DbName     string
    32  	ColName    string
    33  	DataBase   *mongo.Database
    34  	Collection *mongo.Collection
    35  }
    36  
    37  func (b *BaseCollectionImpl) SelectPage(ctx context.Context, filter interface{}, sort interface{}, skip, limit int64) (int64, []interface{}, error) {
    38  	var err error
    39  
    40  	resultCount, err := b.Collection.CountDocuments(ctx, filter)
    41  	if err != nil {
    42  		return 0, nil, err
    43  	}
    44  
    45  	opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
    46  	finder, err := b.Collection.Find(ctx, filter, opts)
    47  	if err != nil {
    48  		return resultCount, nil, err
    49  	}
    50  
    51  	result := make([]interface{}, 0)
    52  	if err := finder.All(ctx, &result); err != nil {
    53  		return resultCount, nil, err
    54  	}
    55  	return resultCount, result, nil
    56  }
    57  
    58  func (b *BaseCollectionImpl) SelectList(ctx context.Context, filter interface{}, sort interface{}) ([]interface{}, error) {
    59  	var err error
    60  
    61  	opts := options.Find().SetSort(sort)
    62  	finder, err := b.Collection.Find(ctx, filter, opts)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	result := make([]interface{}, 0)
    67  	if err := finder.All(ctx, &result); err != nil {
    68  		return nil, err
    69  	}
    70  	return result, err
    71  }
    72  
    73  func (b *BaseCollectionImpl) SelectListForExpect(ctx context.Context, filter interface{}, sort interface{}, expect interface{}, limit int64) (interface{}, error) {
    74  	var err error
    75  	opts := options.Find().SetSort(sort).SetLimit(limit)
    76  	finder, err := b.Collection.Find(ctx, filter, opts)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	if err := finder.All(ctx, &expect); err != nil {
    81  		return nil, err
    82  	}
    83  	return expect, err
    84  }
    85  
    86  func (b *BaseCollectionImpl) SelectOne(ctx context.Context, filter interface{}) (interface{}, error) {
    87  	result := new(interface{})
    88  	err := b.Collection.FindOne(ctx, filter, options.FindOne()).Decode(result)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	return result, nil
    93  }
    94  
    95  func (b *BaseCollectionImpl) SelectCount(ctx context.Context, filter interface{}) (int64, error) {
    96  	return b.Collection.CountDocuments(ctx, filter)
    97  }
    98  
    99  func (b *BaseCollectionImpl) UpdateOne(ctx context.Context, filter, update interface{}) (int64, error) {
   100  	result, err := b.Collection.UpdateOne(ctx, filter, update, options.Update())
   101  	if err != nil {
   102  		return 0, err
   103  	}
   104  	if result.MatchedCount == 0 {
   105  		return 0, fmt.Errorf("Update result: %s ", "document not found")
   106  	}
   107  	return result.MatchedCount, nil
   108  }
   109  
   110  func (b *BaseCollectionImpl) UpsertOne(ctx context.Context, filter, update interface{}) (int64, error) {
   111  	result, err := b.Collection.UpdateOne(ctx, filter, bson.M{"$set": update}, options.Update().SetUpsert(true))
   112  	if err != nil {
   113  		return 0, err
   114  	}
   115  
   116  	return result.MatchedCount, nil
   117  }
   118  
   119  func (b *BaseCollectionImpl) UpdateMany(ctx context.Context, filter, update interface{}) (int64, error) {
   120  	result, err := b.Collection.UpdateMany(ctx, filter, update, options.Update())
   121  	if err != nil {
   122  		return 0, err
   123  	}
   124  	if result.MatchedCount == 0 {
   125  		return 0, fmt.Errorf("Update result: %s ", "document not found")
   126  	}
   127  	return result.MatchedCount, nil
   128  }
   129  
   130  func (b *BaseCollectionImpl) Delete(ctx context.Context, filter interface{}) (int64, error) {
   131  	result, err := b.Collection.DeleteMany(ctx, filter, options.Delete())
   132  	if err != nil {
   133  		return 0, err
   134  	}
   135  	if result.DeletedCount == 0 {
   136  		return 0, fmt.Errorf("DeleteOne result: %s ", "document not found")
   137  	}
   138  	return result.DeletedCount, nil
   139  }
   140  
   141  func (b *BaseCollectionImpl) InsetOne(ctx context.Context, model interface{}) (interface{}, error) {
   142  	result, err := b.Collection.InsertOne(ctx, model, options.InsertOne())
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	return result.InsertedID, err
   147  }
   148  
   149  func (b *BaseCollectionImpl) InsertMany(ctx context.Context, models []interface{}) ([]interface{}, error) {
   150  	result, err := b.Collection.InsertMany(ctx, models, options.InsertMany())
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  	return result.InsertedIDs, err
   155  }
   156  
   157  func (b *BaseCollectionImpl) InsertManyIgnore(ctx context.Context, models []interface{}) ([]interface{}, error) {
   158  	result, err := b.Collection.InsertMany(ctx, models, options.InsertMany().SetOrdered(false))
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	return result.InsertedIDs, err
   163  }
   164  
   165  func (b *BaseCollectionImpl) Aggregate(ctx context.Context, pipeline interface{}, result interface{}) error {
   166  	finder, err := b.Collection.Aggregate(ctx, pipeline, options.Aggregate())
   167  	if err != nil {
   168  		return err
   169  	}
   170  	if err := finder.All(ctx, &result); err != nil {
   171  		return err
   172  	}
   173  	return nil
   174  }
   175  
   176  func (b *BaseCollectionImpl) CreateIndexes(ctx context.Context, indexes []mongo.IndexModel) error {
   177  	_, err := b.Collection.Indexes().CreateMany(ctx, indexes, options.CreateIndexes())
   178  	return err
   179  }
   180  
   181  func (b *BaseCollectionImpl) GetCollection() *mongo.Collection {
   182  	return b.Collection
   183  }