1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mongo_coll.go 6.99 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.5

搜索帮助

0d507c66 1850385 C8b1a773 1850385