mongorm

package module
v0.0.1-beta.1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

mongorm

基于 go.mongodb.org/mongo-driver 二次封装,可以进行链式操作的简单orm

支持操作

  • 搜索

    • where
      where 条件支持andor,关键字分别为whereorWhere。 通过字段操作符进行搜索条件的输入。
    where := mongorm.
        Where("create_time", ">", 1545623340).
        Where("_id", "==", mongorm.ObjectID("5c20575e716de1ba769cc295")).
        Result()
    
    • WhereOrResult
      当出现两个条件出现存在时,需要构建出两个and条件,然后进行or组合
        whereOne := mongorm.
        	Where("create_time", ">", 1545623340)
        whereTwo := mongorm.
        	Where("_id", "==", mongorm.ObjectID("5c20575e716de1ba769cc295"))
        orWhere := mongorm.WhereOrResult(whereOne, whereTwo)
    

操作符号支持如下输入:==>>=<<=!=inINninNINnot inNOT INregexREGEX

其他操作可以通过default选项进行输入,注意保证输入的正确性。

  • 属性设置

在查询时,需要分页,排序,强制命中索引时,可通过对应方法设置需要的内容。同样采用链式操作,进行组合设置。

opt := mongorm.
    SetSelect("order_number,_id,create_time"). // 显示字段
    SetLimit(2).  // 限制条数
    SetOffset(2). // 偏移条数
    SetHint("_id"). // 索引名字
    SetSort("create_time", mongorm.DESC).  // 排序
    SetSort("update_time", mongorm.ASC). // 多条排序
    GetOneOption()

查询一条记录时,结尾使用GetOneOption(),查询多条数据时,结尾使用GetOption()

  • 更新数据

可以同时进行setincr 操作。有个彩蛋是,Result()方法支持传入字段命,用于设置为当前更新时间,比如传入update_time

where := mongorm.
    Where("_id", "==", mongorm.ObjectID("5c20575e716de1ba769cc295")).
    Result()
			
update := mongorm.
	Set("field_time",  time.Now().Unix()).
	Set("field_name",  "测试一下").
	Incr("field_status",  1).
	Incr("field_result",  1).
	Result()
ret := table.NewDemo().Update(where, update)
  • 插入数据

插入一条数据

where := mongorm.NewInsertOne().Value("field_supplier", field.Supplier).Result("update_time", "create_time")

Documentation

Index

Constants

View Source
const (
	Eq  = "="
	Add = "+"
)
View Source
const ASC = SortType(1)
View Source
const DESC = SortType(-1)

Variables

This section is empty.

Functions

func BindData

func BindData(data interface{}, ret interface{}) (err error)

BindData 将data数据绑定到ret的结构体上,注意ret为指针类型 data 数据看中的数据 ret 指针类型的结构体

func GetOneOption

func GetOneOption() *options.FindOneOptions

func GetOption

func GetOption() *options.FindOptions

func NewMongoClient

func NewMongoClient(mongoConf string) (*mongo.Client, error)

func ObjToString

func ObjToString(id primitive.ObjectID) string

func ToBsonD

func ToBsonD(data interface{}) bson.D

ToBsonD 数据转BsonD格式

func ToMap

func ToMap(m bson.M) map[string]interface{}

ToMap bson.M 转 map

func ToObjectID

func ToObjectID(id string) primitive.ObjectID

func WhereOrResult

func WhereOrResult(condition *Condition, secondCondition ...*Condition) bson.M

WhereOrResult 或条件

Types

type Condition

type Condition struct {
	// contains filtered or unexported fields
}

func NewCondition

func NewCondition() *Condition

func Where

func Where(field string, opt string, val interface{}) *Condition

Where 条件

func (*Condition) Cond

func (con *Condition) Cond() bson.D

Cond 输出查询条件

func (*Condition) Result

func (con *Condition) Result() bson.M

Result 输出查询条件,输出bsonM,容易造成数据丢失 Deprecated

func (*Condition) Where

func (con *Condition) Where(field string, opt string, val interface{}) *Condition

func (*Condition) WhereOrResult

func (con *Condition) WhereOrResult(condition *Condition, secondCondition ...*Condition) bson.M

type Conf

type Conf struct {
	URI string `json:"uri"`
}

type Insert

type Insert struct {
	// contains filtered or unexported fields
}

func NewInsertOne

func NewInsertOne() *Insert

func (*Insert) Result

func (in *Insert) Result(autoFields ...string) bson.D

func (*Insert) ResultRAW

func (in *Insert) ResultRAW(autoFields ...string) map[string]interface{}

ResultRAW 输出未bson化前的内容

func (*Insert) Value

func (in *Insert) Value(field string, val interface{}) *Insert
type Search struct {
	Projection string // 返回字段select
	Skip       int64
	Limit      int64
	Sort       bson.D
	Hint       string
}

func NewSearch

func NewSearch() *Search

func SetHint

func SetHint(hint string) *Search

func SetLimit

func SetLimit(limit int64) *Search

func SetOffset

func SetOffset(offset int64) *Search

func SetSelect

func SetSelect(fields string) *Search

func SetSort

func SetSort(field string, sort SortType) *Search

func (*Search) GetOneOption

func (s *Search) GetOneOption() *options.FindOneOptions

func (*Search) GetOption

func (s *Search) GetOption() *options.FindOptions

func (*Search) SetHint

func (s *Search) SetHint(hint string) *Search

func (*Search) SetLimit

func (s *Search) SetLimit(limit int64) *Search

func (*Search) SetOffset

func (s *Search) SetOffset(offset int64) *Search

func (*Search) SetSelect

func (s *Search) SetSelect(fields string) *Search

func (*Search) SetSort

func (s *Search) SetSort(field string, sort SortType) *Search

type SearchSort

type SearchSort struct {
	Field string
	SortType
}

type SortType

type SortType int

type Update

type Update struct {
	// contains filtered or unexported fields
}

func Incr

func Incr(field string, val int) *Update

Incr 字段加val

func NewUpdate

func NewUpdate() *Update

func Set

func Set(field string, val interface{}) *Update

Set 设置更新的字段和内容

func (*Update) Incr

func (up *Update) Incr(field string, val int) *Update

func (*Update) Result

func (up *Update) Result(autoFields ...string) bson.D

func (*Update) Set

func (up *Update) Set(field string, val interface{}) *Update

func (*Update) SetAll

func (up *Update) SetAll(dt map[string]interface{}) *Update

SetAll 设置从map里面批量传入k=>v的参数

func (*Update) Sset

func (up *Update) Sset(opt string, field string, val interface{}) *Update

Sset 特殊的操作

Jump to

Keyboard shortcuts

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