mongoorm

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 6, 2020 License: MIT Imports: 8 Imported by: 0

README

mongoorm

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

支持操作

  • 搜索

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

操作符号支持如下输入:

  • ==
  • >
  • >=
  • <
  • <=
  • !=
  • in
  • IN
  • nin
  • NIN
  • not in
  • NOT IN
  • regex
  • REGEX

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

  • option

在查询时,需要分页,排序,强制命中索引时,需要传入option内容。同样采用链式操作,进行组合设置。

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

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

  • 更新数据

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

where := mongoorm.
    Where("_id", "==", mongoorm.ToObjectID("5c20575e716de1ba769cc295")).
    Result()
			
update := mongoorm.
	Set("seat_time",  time.Now().Unix()).
	Set("seat_name",  "测试一下").
	Incr("pay_status",  1).
	Incr("process_result",  1).
	Result()
ret := maicailog.NewMonitorLog().Update(where, update)
  • 插入数据

插入一条数据

where := mongoorm.NewInsertOne().Value("tencent_supplier", tencent.SUPPLIER).Result("update_time", "create_time")

Documentation

Index

Constants

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)

func GetOneOption

func GetOneOption() *options.FindOneOptions

func GetOption

func GetOption() *options.FindOptions

func NewMongoClient

func NewMongoClient(mongoConf string) *mongo.Client

func ObjToString

func ObjToString(id primitive.ObjectID) string

func ToBsonD

func ToBsonD(data interface{}) bson.D

func ToMap

func ToMap()

func ToObjectID

func ToObjectID(id string) primitive.ObjectID

func WhereOrResult

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

where or

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) Result

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

输出查询条件

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 {
	Hosts             string `json:"hosts"`
	MinPoolSize       uint64 `json:"min_pool_size"`
	MaxPoolSize       uint64 `json:"max_pool_size"`
	HeartBeatInterval int    `json:"heart_beat_interval"`
	MaxConnIdleTime   int    `json:"max_conn_idle_time"`
	APPName           string `json:"app_name"`
}

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) 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

func NewUpdate

func NewUpdate() *Update

func Set

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

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

Jump to

Keyboard shortcuts

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