mongo

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: May 5, 2021 License: Apache-2.0 Imports: 9 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 Client

type Client interface {
	GetSession() *mgo.Session
	Close()
	Do(model Model, exec func(s *mgo.Collection) error) error
	GetConfig() Config
}

func Dial

func Dial(url string) (Client, error)

example: mongodb://myuser:mypass@localhost:27017,otherhost:27017/db

func Init

func Init(config Config) (Client, error)

func InitFast

func InitFast(url string) (Client, error)

func NewClient

func NewClient(config Config) (Client, error)

type Config

type Config struct {
	Addrs          []string      `json:"addrs"`
	Database       string        `json:"database"`
	Username       string        `json:"username"`
	Password       string        `json:"password"`
	Source         string        `json:"source"`
	ReplicaSetName string        `json:"replica_set_name"`
	Timeout        time.Duration `json:"timeout"`
	Mode           mgo.Mode      `json:"mode"`
	PoolLimit      int           `json:"pool_limit"`
	MaxIdleTime    time.Duration `json:"max_idle_time"`
	AppName        string        `json:"app_name"`
	InsertTimeAuto bool          `json:"insert_time_auto"`
	UpdateTimeAuto bool          `json:"update_time_auto"`
}

type Model

type Model interface {
	Database() string
	Collection() string
}

type MongoDB

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

func New

func New(database string, collection string, value interface{}) *MongoDB

func (*MongoDB) Client added in v0.1.4

func (db *MongoDB) Client() Client

func (*MongoDB) Collection

func (db *MongoDB) Collection() string

func (*MongoDB) Count

func (db *MongoDB) Count(selector interface{}) (count int, err error)

func (*MongoDB) Database

func (db *MongoDB) Database() string

func (*MongoDB) Distinct

func (db *MongoDB) Distinct(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 (*MongoDB) Do

func (db *MongoDB) Do(f func(c *mgo.Collection) error) error

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

func (*MongoDB) FindAll

func (db *MongoDB) FindAll(selector interface{}, sort []string, picker []string, skip int, limit int, ret interface{}) error

func (*MongoDB) FindId

func (db *MongoDB) FindId(id interface{}, picker []string, ret interface{}) error

func (*MongoDB) FindObjectId

func (db *MongoDB) FindObjectId(id string, picker []string, ret interface{}) error

func (*MongoDB) FindOne

func (db *MongoDB) FindOne(selector interface{}, picker []string, ret interface{}) error

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

func (*MongoDB) FindOneWithSort

func (db *MongoDB) FindOneWithSort(selector interface{}, sort []string, picker []string, ret interface{}) error

func (*MongoDB) Insert

func (db *MongoDB) Insert(doc ...interface{}) error

func (*MongoDB) Modify

func (db *MongoDB) Modify(selector, doc bson.M, ret interface{}, deletion ...bool) error

func (*MongoDB) ModifyAll

func (db *MongoDB) ModifyAll(selector, doc bson.M, deletion ...bool) (changeInfo *mgo.ChangeInfo, err error)

func (*MongoDB) ModifyId

func (db *MongoDB) ModifyId(id interface{}, doc bson.M, ret interface{}, deletion ...bool) error

func (*MongoDB) PipeAll

func (db *MongoDB) PipeAll(selector interface{}, ret interface{}) error

func (*MongoDB) Remove

func (db *MongoDB) Remove(selector interface{}) error

func (*MongoDB) RemoveAll

func (db *MongoDB) RemoveAll(selector interface{}) (changeInfo *mgo.ChangeInfo, err error)

func (*MongoDB) RemoveID

func (db *MongoDB) RemoveID(id interface{}) error

func (*MongoDB) Replace

func (db *MongoDB) Replace(selector, update interface{}) error

Replace replace the original document as a whole, but the value of create_time is the value of the old document

func (*MongoDB) ReplaceAll

func (db *MongoDB) ReplaceAll(selector, update interface{}) (changeInfo *mgo.ChangeInfo, err error)

func (*MongoDB) ReplaceId

func (db *MongoDB) ReplaceId(id, update interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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