Mongo

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AdapterClass = "@pgo/Client/Mongo/Adapter"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	pgo.Object
	// contains filtered or unexported fields
}

Adapter of Mongo Client, add context support. usage: mongo := this.GetObject(Mongo.AdapterClass, db, coll).(*Mongo.Adapter)

func (*Adapter) Construct

func (a *Adapter) Construct(db, coll string, componentId ...string)

func (*Adapter) Count

func (a *Adapter) Count(query interface{}, options ...bson.M) (int, error)

Count return the count of documents match the query.

func (*Adapter) DeleteAll

func (a *Adapter) DeleteAll(query interface{}) error

DeleteAll delete all documents that match the query.

func (*Adapter) DeleteOne

func (a *Adapter) DeleteOne(query interface{}) error

DeleteOne delete one document that match the query.

func (*Adapter) FindAll

func (a *Adapter) FindAll(query interface{}, result interface{}, options ...bson.M) error

FindAll retrieve all documents that match the query, param result must be a slice(interface{}, map, bson.M or bson compatible struct) other params see FindOne()

func (*Adapter) FindAndModify

func (a *Adapter) FindAndModify(query interface{}, change mgo.Change, result interface{}, options ...bson.M) error

FindAndModify execute findAndModify command, which allows atomically update or remove one document, param change specify the change operation, eg. mgo.Change{Update:bson.M{"$inc": bson.M{"n":1}}, ReturnNew:true}, other params see FindOne()

func (*Adapter) FindDistinct

func (a *Adapter) FindDistinct(query interface{}, key string, result interface{}, options ...bson.M) error

FindDistinct retrieve distinct values for the param key, param result must be a slice, other params see FindOne()

func (*Adapter) FindOne

func (a *Adapter) FindOne(query interface{}, result interface{}, options ...bson.M) error

FindOne retrieve the first document that match the query, query can be a map or bson compatible struct, such as bson.M or properly typed map, nil query is equivalent to empty query such as bson.M{}. result is pointer to interface{}, map, bson.M or bson compatible struct, if interface{} type is provided, the output result is a bson.M. options provided optional query option listed as follows: fields: bson.M, set output fields, eg. bson.M{"_id":0, "name":1}, sort: string or []string, set sort order, eg. "key1" or []string{"key1", "-key2"}, skip: int, set skip number, eg. 100, limit: int, set result limit, eg. 1, hint: string or []string, set index hint, eg. []string{"key1", "key2"}

for example:

var v1 interface{} // type of output v1 is bson.M
m.FindOne(bson.M{"_id":"k1"}, &v1)

var v2 struct {
    Id    string `bson:"_id"`
    Name  string `bson:"name"`
    Value string `bson:"value"`
}
m.FindOne(bson.M{"_id": "k1"}, &v2)

func (*Adapter) GetClient

func (a *Adapter) GetClient() *Client

func (*Adapter) InsertAll

func (a *Adapter) InsertAll(docs []interface{}) error

InsertAll insert all documents provided by params docs into collection, for example:

docs := []interface{}{
    bson.M{"_id":1, "name":"v1"},
    bson.M{"_id":2, "name":"v2"},
}
a.InsertAll(docs)

func (*Adapter) InsertOne

func (a *Adapter) InsertOne(doc interface{}) error

InsertOne insert one document into collection, param doc can be a map, bson.M, bson compatible struct, for example:

a.InsertOne(bson.M{"field1":"value1", "field2":"value2"})

doc := struct {
    Field1 string `bson:"field1"`
    Field2 string `bson:"field2"`
} {"value1", "value2"}
a.InsertOne(doc)

func (*Adapter) MapReduce

func (a *Adapter) MapReduce(query interface{}, job *mgo.MapReduce, result interface{}, options ...bson.M) error

MapReduce execute map/reduce job that match the query. param result is a slice(interface{}, map, bson.M, bson compatible struct), param query and options see FindOne(). for example:

job := &mgo.MapReduce{
    Map: "function() { emit(this.n, 1) }",
    Reduce: "function(key, values) { return Array.sum(values) }",
}
result := []bson.M{}
a.MapReduce(query, job, &result)

func (*Adapter) PipeAll

func (a *Adapter) PipeAll(pipeline interface{}, result interface{}) error

PipeAll execute aggregation queries and get all item from result set. param result must be slice(interface{}, map, bson.M or bson compatible struct). see PipeOne().

func (*Adapter) PipeOne

func (a *Adapter) PipeOne(pipeline interface{}, result interface{}) error

PipeOne execute aggregation queries and get the first item from result set. param pipeline must be a slice, such as []bson.M, param result is a pointer to interface{}, map, bson.M or bson compatible struct. for example:

pipeline := []bson.M{
    bson.M{"$match": bson.M{"status":"A"}},
    bson.M{"$group": bson.M{"_id":"$field1", "total":"$field2"}},
}
a.PipeOne(pipeline, &result)

func (*Adapter) UpdateAll

func (a *Adapter) UpdateAll(query interface{}, update interface{}) error

UpdateAll update all documents that match the query, see UpdateOne()

func (*Adapter) UpdateOne

func (a *Adapter) UpdateOne(query interface{}, update interface{}) error

UpdateOne update one document that match the query, mgo.ErrNotFound is returned if a document not found, a value of *LastError is returned if other error occurred.

func (*Adapter) UpdateOrInsert

func (a *Adapter) UpdateOrInsert(query interface{}, update interface{}) error

UpdateOrInsert update a existing document that match the query, or insert a new document base on the update document if no document match, an error of *LastError is returned if error is detected.

type Client

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

Mongo Client component, configuration: mongo:

class: "@pgo/Client/Mongo/Client"
dsn: "mongodb://host1:port1/[db][?options]"
connectTimeout: "1s"
readTimeout: "10s"
writeTimeout: "10s"

see Dial() for query options, default: replicaSet= connect=replicaSet maxPoolSize=100 minPoolSize=1 maxIdleTimeMS=300000 ssl=false w=1 j=false wtimeoutMS=10000 readPreference=secondaryPreferred

func (*Client) Construct

func (c *Client) Construct()

func (*Client) GetSession

func (c *Client) GetSession() *mgo.Session

func (*Client) Init

func (c *Client) Init()

func (*Client) SetConnectTimeout

func (c *Client) SetConnectTimeout(v string)

func (*Client) SetDsn

func (c *Client) SetDsn(dsn string)

func (*Client) SetReadTimeout

func (c *Client) SetReadTimeout(v string)

func (*Client) SetWriteTimeout

func (c *Client) SetWriteTimeout(v string)

Jump to

Keyboard shortcuts

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