v1

package
v1.4.10 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NullRet = &struct{}{}
	NullDoc = make(bson.M)
	NullMap = make(map[string]interface{})
)

Functions

This section is empty.

Types

type Base

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

func NewBaseModel

func NewBaseModel(client Client, database string, collection string) *Base

func (*Base) Client

func (db *Base) Client() Client

func (*Base) Collection

func (db *Base) Collection() string

func (*Base) Count

func (db *Base) Count(ctx context.Context, selector interface{}) (count int, err error)

func (*Base) Database

func (db *Base) Database() string

func (*Base) DeleteAll

func (db *Base) DeleteAll(ctx context.Context, selector interface{}) (changeInfo *mgo.ChangeInfo, err error)

func (*Base) DeleteId

func (db *Base) DeleteId(ctx context.Context, id string) error

func (*Base) DeleteOne

func (db *Base) DeleteOne(ctx context.Context, selector interface{}) error

func (*Base) Distinct

func (db *Base) Distinct(ctx context.Context, selector interface{}, key string) ([]interface{}, error)

Distinct unmarshals into result the list of distinct values for the given key.

For example:

ret, err = db.Distinct(bson.M{"gender": 1}, "age")
fmt.Println(ret)

DB:

		{ ObjectId("603a081694ea2e906792a8f1"), name:"a", gender:"1", age:12 }
		{ ObjectId("603a081694ea2e906792a8f2"), name:"b", gender:"1", age:13 }
		{ ObjectId("603a081694ea2e906792a8f3"), name:"c", gender:"1", age:14 }
		{ ObjectId("603a081694ea2e906792a8f4"), name:"d", gender:"1", age:15 }
		{ ObjectId("603a081694ea2e906792a8f5"), name:"e", gender:"1", age:14 }
 	{ ObjectId("603a081694ea2e906792a8f6"), name:"f", gender:"1", age:13 }

Console:

[12, 13, 14 ,15]

func (*Base) Do

func (db *Base) Do(ctx context.Context, f func(c *mgo.Collection) error) error

Do it is used for you to use the nativer v1 interface according to your own needs, Use when you can't find the method you want in this package

func (*Base) FindAll

func (db *Base) FindAll(ctx context.Context, selector interface{}, sort []string, picker []string, skip int, limit int, result interface{}) error

func (*Base) FindId

func (db *Base) FindId(ctx context.Context, id string, picker []string, result interface{}) error

func (*Base) FindOne

func (db *Base) FindOne(ctx context.Context, selector interface{}, picker []string, result interface{}) error

FindOne the param picker([]string) represents the field to return

func (*Base) FindOneWithSort

func (db *Base) FindOneWithSort(ctx context.Context, selector interface{}, sort []string, picker []string, result interface{}) error

func (*Base) Indexes

func (db *Base) Indexes() ([]mgo.Index, error)

func (*Base) Insert

func (db *Base) Insert(ctx context.Context, doc ...interface{}) error

func (*Base) PipeAll

func (db *Base) PipeAll(ctx context.Context, pipeline interface{}, ret interface{}) error

func (*Base) RemoveAll

func (db *Base) RemoveAll(ctx context.Context, selector interface{}) (changeInfo *mgo.ChangeInfo, err error)

func (*Base) RemoveId

func (db *Base) RemoveId(ctx context.Context, id string) error

func (*Base) RemoveOne

func (db *Base) RemoveOne(ctx context.Context, selector interface{}) error

func (*Base) UpdateAll

func (db *Base) UpdateAll(ctx context.Context, selector, update interface{}) (changeInfo *mgo.ChangeInfo, err error)

func (*Base) UpdateId

func (db *Base) UpdateId(ctx context.Context, id string, update interface{}) error

func (*Base) UpdateOne

func (db *Base) UpdateOne(ctx context.Context, selector, update interface{}) error

func (*Base) Upsert

func (db *Base) Upsert(ctx context.Context, selector, update interface{}) (changeInfo *mgo.ChangeInfo, err error)

func (*Base) UpsertId

func (db *Base) UpsertId(ctx context.Context, id string, update interface{}) (changeInfo *mgo.ChangeInfo, err error)

type Client

type Client interface {
	Kernel() *mgo.Session
	Close()
	Do(ctx context.Context, model Model, exec func(s *mgo.Collection) error) error
	Config() Config
	NewBaseModel(database string, collection string) *Base
}

func Dial

func Dial(url string) (Client, error)

Dial example: mongodb://username:password@localhost:27017,otherhost:27017/db, default auto_time is true

func GetGlobalClient

func GetGlobalClient() Client

func NewClient

func NewClient(config Config) (Client, error)

func NewGlobalClient

func NewGlobalClient(config Config) (Client, error)

NewGlobalClient If there is only one Mongo, you can select the global client

type Config

type Config struct {
	Addrs          []string `mapstructure:"addrs" json:"addrs"`
	Database       string   `mapstructure:"db" json:"db"`
	Username       string   `mapstructure:"username" json:"username"`
	Password       string   `mapstructure:"password" json:"password"`
	Source         string   `mapstructure:"source" json:"source"`
	ReplicaSetName string   `mapstructure:"replica_set_name" json:"replica_set_name"`
	// Timeout the unit is seconds
	Timeout   time.Duration `mapstructure:"timeout" json:"timeout"`
	Mode      mgo.Mode      `mapstructure:"mode" json:"mode"`
	PoolLimit int           `mapstructure:"pool_limit" json:"pool_limit"`
	// MaxIdleTime The maximum number of seconds to remain idle in the pool, default 20 minutes
	MaxIdleTime time.Duration `mapstructure:"max_idle_time" json:"max_idle_time"`
	AppName     string        `mapstructure:"app_name" json:"app_name"`
	// Alias If it is empty, it will not be included in the client Map management
	// 如果为空则不纳入clientMap管理
	Alias string `mapstructure:"alias" json:"alias"`
}

type Manager

type Manager interface {
	Add(alias string, c Client)
	Delete(alias string)
	Get(alias string) (Client, error)
	Has(alias string) bool
}

func ClientManager

func ClientManager() Manager

type Model

type Model interface {
	Database() string
	Collection() string
	Indexes() ([]mgo.Index, error)
}

Jump to

Keyboard shortcuts

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