crud

package
v1.230621.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SetOnInsert = "setOnInsert"
)

Variables

View Source
var Upsert = true

Functions

func GenerateId added in v1.0.5

func GenerateId(resource *Resource, key string, initVal int64) int64

生成id

func NewStruct

func NewStruct(name string) interface{}

根据类型名称初始化类型

func RegisterType

func RegisterType(elem interface{})

注册类型

func ToMap

func ToMap(in interface{}, tagName string) (map[string]interface{}, error)

Types

type DeleteOption

type DeleteOption struct {
	// 集合名称
	CollectionName string
	// 过滤字段,如果设置则以字段为查询条件进行修改,如果都没有值则失败
	Filter []Filter
}

集合的定义

type Filter

type Filter struct {
	Key      string
	Operator string
	Value    interface{}
}

过滤类型

type FindOneOptions

type FindOneOptions struct {
	// 集合名称
	CollectionName *string
	// 排序规则,默认是按照 _id 降序排序
	Sort []Sort
}

func CreateFindOneOptions

func CreateFindOneOptions(collectionName string) *FindOneOptions

func (*FindOneOptions) SetSort

func (op *FindOneOptions) SetSort(sort ...Sort) *FindOneOptions

type FindOptions

type FindOptions struct {
	commons.PagingRequest

	FindOneOptions

	// 返回的数据
	Results interface{}
}

func (*FindOptions) SetCollectionName

func (op *FindOptions) SetCollectionName(colName string) *FindOptions

func (*FindOptions) SetSort

func (op *FindOptions) SetSort(sort ...Sort) *FindOptions

type Inc

type Inc struct {
	Key   string
	Value int64
}

type Operator

type Operator string
const (
	OP_IN  Operator = "$in"
	OP_NIN Operator = "$nin"

	OP_EQ Operator = "$eq"
	OP_NE Operator = "$ne"

	OP_GT  Operator = "$gt"
	OP_GTE Operator = "$gte"

	OP_LT  Operator = "$lt"
	OP_LTE Operator = "$lte"

	OP_L_AND Operator = "$and"
	OP_L_NOT Operator = "$not"
	OP_L_NOR Operator = "$nor"
	OP_L_OR  Operator = "$or"

	OP_EL_EXISTS Operator = "$exists"
	OP_EL_TYPE   Operator = "$type"

	OP_EVA_EXPR       Operator = "$expr"
	OP_EVA_JSONSCHEMA Operator = "$jsonSchema"
	OP_EVA_MOD        Operator = "$mod"
	OP_EVA_REGEX      Operator = "$regex"
	OP_EVA_TEXT       Operator = "$text"
	OP_EVA_WHERE      Operator = "$where"

	OP_GEO_INTERSECTS  Operator = "$geoIntersects"
	OP_GEO_WITHIN      Operator = "$geoWithin"
	OP_GEO_NEAR        Operator = "$near"
	OP_GEO_NEAR_SPHERE Operator = "$nearSphere"

	OP_ARRAY_ELEMMATCH Operator = "$elemMatch"
)

type Resource

type Resource struct {
	DB     *mongo.Database
	Client *mongo.Client
}
var DbTestInstance *Resource

func NewDB

func NewDB(uri string, databaseName string) (*Resource, error)

初始化数据库链接

func SimpleNewDB

func SimpleNewDB() (*Resource, error)

根据环境变量快速初始化数据库链接

func (*Resource) DeleteAny

func (instance *Resource) DeleteAny(deleteOption *DeleteOption) error

根据条件删除数据

func (*Resource) DeleteById

func (instance *Resource) DeleteById(tableName string, id primitive.ObjectID) error

根据主键删除数据

func (*Resource) Find

func (instance *Resource) Find(filter bson.M, opts FindOptions) *commons.PagingResponse

@param filterJSON mongo查询语句

func (*Resource) FindById

func (instance *Resource) FindById(id string, result interface{}, opts FindOneOptions)

根据id查找元素

@param id 数据主键

@param result 返回的数据对象,比如 &user

func (*Resource) FindOne

func (instance *Resource) FindOne(filter bson.M, result interface{}, opts FindOneOptions)

@param filterJSON mongo查询语句

func (*Resource) FindOneWithBson

func (instance *Resource) FindOneWithBson(filterJSON string, result interface{}, opts FindOneOptions)

@param filterJSON mongo查询语句

func (*Resource) FindWithBson

func (instance *Resource) FindWithBson(filterJSON string, opts FindOptions) *commons.PagingResponse

@param filterJSON mongo查询语句

func (*Resource) FindWithoutPaging

func (instance *Resource) FindWithoutPaging(filter bson.M, opts FindOptions) []bson.M

@param filterJSON mongo查询语句

func (*Resource) Query

func (instance *Resource) Query(pipeline []bson.D, opts FindOptions) *commons.PagingResponse

Example usage:

mongo.Pipeline{
	{{"$group", bson.D{{"_id", "$state"}, {"totalPop", bson.D{{"$sum", "$pop"}}}}}},
	{{"$match", bson.D{{"totalPop", bson.D{{"$gte", 10*1000*1000}}}}}},
}

func (*Resource) QueryAllowDiskUse added in v1.1.4

func (instance *Resource) QueryAllowDiskUse(pipeline []bson.D, opts FindOptions, allowDiskUse bool) *commons.PagingResponse

func (*Resource) QueryWithBson

func (instance *Resource) QueryWithBson(filterJSON string, opts FindOptions) *commons.PagingResponse

func (*Resource) QueryWithoutPaging

func (instance *Resource) QueryWithoutPaging(pipeline []bson.D, opts FindOptions) ([]bson.M, error)

func (*Resource) SaveOrUpdateOne

func (instance *Resource) SaveOrUpdateOne(u interface{}, updateOptions ...*UpdateOption) (interface{}, error)

修改一个数据

type Sort

type Sort struct {
	// 排序字段
	Key string `json:"key"`
	// -1 是降序,1是升序
	Sort int64 `json:"sort"`
}

type UpdateOption

type UpdateOption struct {
	// 集合名称
	CollectionName *string
	// 过滤字段,如果设置则以字段为查询条件进行修改,如果都没有值则失败
	Filter []string
	// 自增字段
	Inc []Inc
}

集合的定义

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL