代码拉取完成,页面将自动刷新
package mongo
import (
"context"
"log"
"gitee.com/carlmax_my/console-core-go/pkg/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Collection[T interface{}] struct {
mongo.Collection
}
type collOpts struct {
collName string
}
type OptFunc func(opt *collOpts)
func SetColl(collName string) OptFunc {
return func(opt *collOpts) {
opt.collName = collName
}
}
func NewColl[T interface{}](db MongoDB, optFuncs ...OptFunc) (c *Collection[T]) {
opt := &collOpts{
collName: "",
}
for _, optFunc := range optFuncs {
optFunc(opt)
}
if opt.collName == "" {
log.Fatal("[mongo]no col name")
}
coll := db.GetDb().Collection(opt.collName) // new every time?
c = &Collection[T]{
Collection: *coll,
}
return
}
func (c *Collection[T]) DB() *mongo.Database {
return c.Database()
}
func (c *Collection[T]) DropCollection() {
if c != nil {
ctx := context.Background()
c.Drop(ctx)
}
}
func (c *Collection[T]) Create(doc *T) error {
return c.InsertOne(doc)
}
// insert one with full doc, so we can use T, BUT UPDATE IS PARTIAL
func (c *Collection[T]) InsertOne(doc *T) (err error) {
ctx := context.Background()
_, err = c.Collection.InsertOne(ctx, doc)
return
}
// insert one with full doc, so we can use T, BUT UPDATE IS PARTIAL
func (c *Collection[T]) InsertOneGeneral(doc interface{}) (err error) {
ctx := context.Background()
_, err = c.Collection.InsertOne(ctx, doc)
return
}
func (c *Collection[T]) InsertMany(doc []interface{}) (err error) {
ctx := context.Background()
_, err = c.Collection.InsertMany(ctx, doc)
return
}
func (c *Collection[T]) UpdateOne(filter interface{}, update interface{}) (err error) {
ctx := context.Background()
_, err = c.Collection.UpdateOne(ctx, filter, update)
return
}
func (c *Collection[T]) DeleteOneByFilter(filter interface{}) (err error) {
ctx := context.Background()
_, err = c.Collection.DeleteOne(ctx, filter)
return
}
func (c *Collection[T]) DeleteAllByFilter(filter interface{}) (err error) {
ctx := context.Background()
_, err = c.Collection.DeleteMany(ctx, filter)
return
}
func (c *Collection[T]) DeleteById(id *string) error {
objId, err := primitive.ObjectIDFromHex(*id)
if err != nil {
return err
}
ctx := context.Background()
filter := bson.M{"_id": objId}
if _, err := c.Collection.DeleteOne(ctx, filter); err != nil {
return err
}
return nil
}
func (c *Collection[T]) ExistById(id string) (bool, error) {
if r, err := c.FindOneByIdStr(id); err != nil {
return false, err
} else {
return r != nil, nil
}
}
func (c *Collection[T]) FindOneById(id primitive.ObjectID, opts ...*options.FindOneOptions) (*T, error) {
res := new(T)
util.SetDefault(res)
ctx := context.Background()
filter := bson.M{"_id": id}
if err := c.Collection.FindOne(ctx, filter, opts...).Decode(res); err != nil {
if isNoDocumentsErr(err) {
// log.Println("FindOneById", err.Error())
return nil, nil
}
return nil, err
}
return res, nil
}
func (c *Collection[T]) FindOneByIdStr(id string, opts ...*options.FindOneOptions) (*T, error) {
res := new(T)
util.SetDefault(res)
ctx := context.Background()
filter := bson.M{"_id": id}
if err := c.Collection.FindOne(ctx, filter, opts...).Decode(res); err != nil {
if isNoDocumentsErr(err) {
// log.Println("FindOneByIdStr", err.Error())
return nil, nil
}
return nil, err
}
return res, nil
}
func (c *Collection[T]) FindOneByFilter(filter interface{}, opts ...*options.FindOneOptions) (*T, error) {
res := new(T)
util.SetDefault(res)
ctx := context.Background()
if err := c.Collection.FindOne(ctx, filter, opts...).Decode(res); err != nil {
if isNoDocumentsErr(err) {
// log.Println("FindOneByFilter", err.Error())
return nil, nil
}
return nil, err
}
return res, nil
}
func (c *Collection[T]) FindAll(opts ...*options.FindOptions) (all []T, err error) {
return c.FindAllByFilter(&bson.M{}, opts...)
}
func (c *Collection[T]) FindAllByFilter(filter interface{}, opts ...*options.FindOptions) (all []T, err error) {
all = make([]T, 0)
ctx := context.Background()
cursor, err := c.Collection.Find(ctx, filter, opts...)
if err != nil {
log.Println("FindAllByFilter", err.Error())
return nil, err
}
for cursor.Next(context.TODO()) {
var result T
util.SetDefault(&result)
if err := cursor.Decode(&result); err == nil {
all = append(all, result)
} else {
log.Println("FindAllByFilter decode err:", err)
}
}
if err := cursor.Err(); err != nil {
log.Println("FindAllByFilter cursor err:", err)
}
return
}
// page start from 0
func (c *Collection[T]) FindAllPagedByFilter(filter interface{}, page int64, pageSize int64) ([]T, error) {
skip := page * pageSize
return c.FindAllByFilter(filter, &options.FindOptions{
Skip: &skip,
Limit: &pageSize,
})
}
func (c *Collection[T]) UpdateOneById(objId primitive.ObjectID, doc interface{}) error {
update := bson.M{
"$set": doc,
}
filter := bson.M{
"_id": objId,
}
if err := c.UpdateOne(filter, update); err != nil {
return err
}
return nil
}
func (c *Collection[T]) UpdateOneByIdStr(objId *string, doc interface{}) error {
update := bson.M{
"$set": doc,
}
filter := bson.M{
"_id": *objId,
}
if err := c.UpdateOne(filter, update); err != nil {
return err
}
return nil
}
func (c *Collection[T]) UpdateOneByFilter(filter *bson.M, doc interface{}) error {
update := bson.M{
"$set": doc,
}
if err := c.UpdateOne(*filter, update); err != nil {
return err
}
return nil
}
func (c *Collection[T]) Count(filter interface{}, opts ...*options.CountOptions) (int64, error) {
ctx := context.Background()
ret, err := c.Collection.CountDocuments(ctx, filter, opts...)
if err != nil {
log.Println("Count.err: " + err.Error())
return 0, err
}
return ret, nil
}
func (c *Collection[T]) Total(opts ...*options.CountOptions) (int64, error) {
ctx := context.Background()
filter := bson.M{}
ret, err := c.Collection.CountDocuments(ctx, filter, opts...)
if err != nil {
log.Println("Total.err: " + err.Error())
return 0, err
}
return ret, nil
}
func (c *Collection[T]) Aggregate(filter interface{}, opts ...*options.AggregateOptions) (all []interface{}) {
all = make([]interface{}, 0)
ctx := context.Background()
cursor, err := c.Collection.Aggregate(ctx, filter, opts...)
if err != nil {
log.Println("Aggregate.err: " + err.Error())
return
}
for cursor.Next(context.TODO()) {
var result interface{}
// util.SetDefault(&result)
if err := cursor.Decode(&result); err == nil {
all = append(all, result)
} else {
log.Println("Aggregate decode err:", err)
}
}
if err := cursor.Err(); err != nil {
log.Println("Aggregate cursor err:", err)
}
return
}
func (c *Collection[T]) Distinct(field string, filter interface{}, opts ...*options.DistinctOptions) (all []interface{}, e error) {
ctx := context.Background()
ret, err := c.Collection.Distinct(ctx, field, filter, opts...)
if err != nil {
log.Println("Distinct.err: " + err.Error())
return nil, err
}
return ret, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。