Documentation ¶
Index ¶
- Constants
- type Adapter
- func (a *Adapter) Construct(db, coll string, componentId ...string)
- func (a *Adapter) Count(query interface{}, options ...bson.M) (int, error)
- func (a *Adapter) DeleteAll(query interface{}) error
- func (a *Adapter) DeleteOne(query interface{}) error
- func (a *Adapter) FindAll(query interface{}, result interface{}, options ...bson.M) error
- func (a *Adapter) FindAndModify(query interface{}, change mgo.Change, result interface{}, options ...bson.M) error
- func (a *Adapter) FindDistinct(query interface{}, key string, result interface{}, options ...bson.M) error
- func (a *Adapter) FindOne(query interface{}, result interface{}, options ...bson.M) error
- func (a *Adapter) GetClient() *Client
- func (a *Adapter) InsertAll(docs []interface{}) error
- func (a *Adapter) InsertOne(doc interface{}) error
- func (a *Adapter) MapReduce(query interface{}, job *mgo.MapReduce, result interface{}, options ...bson.M) error
- func (a *Adapter) PipeAll(pipeline interface{}, result interface{}) error
- func (a *Adapter) PipeOne(pipeline interface{}, result interface{}) error
- func (a *Adapter) UpdateAll(query interface{}, update interface{}) error
- func (a *Adapter) UpdateOne(query interface{}, update interface{}) error
- func (a *Adapter) UpdateOrInsert(query interface{}, update interface{}) error
- type Client
Constants ¶
const (
AdapterClass = "@pgo/Client/Mongo/Adapter"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
Adapter of Mongo Client, add context support. usage: mongo := this.GetObject(Mongo.AdapterClass, db, coll).(*Mongo.Adapter)
func (*Adapter) FindAll ¶
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 ¶
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) InsertAll ¶
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 ¶
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 ¶
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 ¶
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) UpdateOne ¶
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 ¶
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