Documentation ¶
Index ¶
- Variables
- func IsDup(err error) bool
- func IsErrNoDocuments(err error) bool
- type A
- type Aggregate
- type Bulk
- func (b *Bulk) InsertOne(doc interface{}) *Bulk
- func (b *Bulk) Remove(filter interface{}) *Bulk
- func (b *Bulk) RemoveAll(filter interface{}) *Bulk
- func (b *Bulk) RemoveId(id interface{}) *Bulk
- func (b *Bulk) Run() (result *qmgo.BulkResult, err error)
- func (b *Bulk) SetOrdered(ordered bool) *Bulk
- func (b *Bulk) UpdateAll(filter interface{}, update interface{}) *Bulk
- func (b *Bulk) UpdateId(id interface{}, update interface{}) *Bulk
- func (b *Bulk) UpdateOne(filter interface{}, update interface{}) *Bulk
- func (b *Bulk) Upsert(filter interface{}, replacement interface{}) *Bulk
- func (b *Bulk) UpsertId(id interface{}, replacement interface{}) *Bulk
- type Collection
- func (c *Collection) Aggregate(ctx context.Context, pipeline interface{}) *Aggregate
- func (c *Collection) BatchInsert(ctx context.Context, docs interface{}) (result *qmgo.InsertManyResult, err error)
- func (c *Collection) Bulk(ctx context.Context) *Bulk
- func (c *Collection) Find(ctx context.Context, filter interface{}) *Query
- func (c *Collection) Insert(ctx context.Context, doc interface{}) (result *qmgo.InsertOneResult, err error)
- func (c *Collection) Remove(ctx context.Context, filter interface{}) (err error)
- func (c *Collection) RemoveAll(ctx context.Context, filter interface{}) (result *qmgo.DeleteResult, err error)
- func (c *Collection) RemoveId(ctx context.Context, id interface{}) (err error)
- func (c *Collection) ReplaceOne(ctx context.Context, filter interface{}, doc interface{}) (err error)
- func (c *Collection) UpdateAll(ctx context.Context, filter interface{}, update interface{}) (result *qmgo.UpdateResult, err error)
- func (c *Collection) UpdateId(ctx context.Context, id interface{}, update interface{}) (err error)
- func (c *Collection) UpdateOne(ctx context.Context, filter interface{}, update interface{}) (err error)
- func (c *Collection) Upsert(ctx context.Context, filter interface{}, replacement interface{}) (result *qmgo.UpdateResult, err error)
- func (c *Collection) UpsertId(ctx context.Context, id interface{}, replacement interface{}) (result *qmgo.UpdateResult, err error)
- type Config
- type D
- type DB
- type E
- type M
- type ObjectID
- type Query
- func (q *Query) All(result interface{}) (err error)
- func (q *Query) Apply(change qmgo.Change, result interface{}) error
- func (q *Query) Count() (n int64, err error)
- func (q *Query) Cursor() qmgo.CursorI
- func (q *Query) Distinct(key string, result interface{}) (err error)
- func (q *Query) Hint(hint interface{}) *Query
- func (q *Query) Limit(n int64) *Query
- func (q *Query) One(result interface{}) (err error)
- func (q *Query) Select(projection interface{}) *Query
- func (q *Query) Skip(n int64) *Query
- func (q *Query) Sort(fields ...string) *Query
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoSuchDocuments return if no document found ErrNoSuchDocuments = qmgo.ErrNoSuchDocuments // NilObjectID is the zero value for ObjectID NilObjectID = primitive.NilObjectID // ErrInvalidHex indicates that a hex string cannot be converted to an ObjectID ErrInvalidHex = primitive.ErrInvalidHex )
Functions ¶
func IsErrNoDocuments ¶ added in v1.3.0
IsErrNoDocuments check if err is no documents, simply call if err == ErrNoSuchDocuments or if err == mongo.ErrNoDocuments
Types ¶
type Aggregate ¶
type Aggregate struct {
// contains filtered or unexported fields
}
func (*Aggregate) All ¶
All iterates the cursor from aggregate and decodes each document into results.
type Bulk ¶
type Bulk struct {
// contains filtered or unexported fields
}
Bulk is context for batching operations to be sent to database in a single bulk write.
Bulk is not safe for concurrent use.
Notes:
Individual operations inside a bulk do not trigger middlewares or hooks at present.
Different from original mgo, the qmgo implementation of Bulk does not emulate bulk operations individually on old versions of MongoDB servers that do not natively support bulk operations.
Only operations supported by the official driver are exposed, that is why InsertMany is missing from the methods.
func (*Bulk) Run ¶
func (b *Bulk) Run() (result *qmgo.BulkResult, err error)
Run executes the collected operations in a single bulk operation.
A successful call resets the Bulk. If an error is returned, the internal queue of operations is unchanged, containing both successful and failed operations.
func (*Bulk) SetOrdered ¶
SetOrdered marks the bulk as ordered or unordered.
If ordered, writes does not continue after one individual write fails. Default is ordered.
func (*Bulk) UpdateAll ¶
UpdateAll queues an UpdateAll operation for bulk execution. The update should contain operator
func (*Bulk) UpdateId ¶
UpdateId queues an UpdateId operation for bulk execution. The update should contain operator
func (*Bulk) UpdateOne ¶
UpdateOne queues an UpdateOne operation for bulk execution. The update should contain operator
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
func (*Collection) Aggregate ¶
func (c *Collection) Aggregate(ctx context.Context, pipeline interface{}) *Aggregate
Aggregate executes an aggregate command against the collection and returns a Aggregate to get resulting documents.
func (*Collection) BatchInsert ¶
func (c *Collection) BatchInsert(ctx context.Context, docs interface{}) (result *qmgo.InsertManyResult, err error)
BatchInsert executes an insert command to insert multiple documents into the collection. Reference: https://docs.mongodb.com/manual/reference/command/insert/
func (*Collection) Bulk ¶
func (c *Collection) Bulk(ctx context.Context) *Bulk
Bulk returns a new context for preparing bulk execution of operations.
func (*Collection) Find ¶
func (c *Collection) Find(ctx context.Context, filter interface{}) *Query
Find find by condition filter,return Query
func (*Collection) Insert ¶
func (c *Collection) Insert(ctx context.Context, doc interface{}) (result *qmgo.InsertOneResult, err error)
InsertOne insert one document into the collection Reference: https://docs.mongodb.com/manual/reference/command/insert/
func (*Collection) Remove ¶
func (c *Collection) Remove(ctx context.Context, filter interface{}) (err error)
Remove executes a delete command to delete at most one document from the collection. if filter is bson.M{},DeleteOne will delete one document in collection Reference: https://docs.mongodb.com/manual/reference/command/delete/
func (*Collection) RemoveAll ¶
func (c *Collection) RemoveAll(ctx context.Context, filter interface{}) (result *qmgo.DeleteResult, err error)
RemoveAll executes a delete command to delete documents from the collection. If filter is bson.M{},all ducuments in Collection will be deleted Reference: https://docs.mongodb.com/manual/reference/command/delete/
func (*Collection) RemoveId ¶
func (c *Collection) RemoveId(ctx context.Context, id interface{}) (err error)
RemoveId executes a delete command to delete at most one document from the collection.
func (*Collection) ReplaceOne ¶
func (c *Collection) ReplaceOne(ctx context.Context, filter interface{}, doc interface{}) (err error)
ReplaceOne executes an update command to update at most one document in the collection. If UpdateHook in opts is set, hook works on it, otherwise hook try the doc as hook Expect type of the doc is the define of user's document
func (*Collection) UpdateAll ¶
func (c *Collection) UpdateAll(ctx context.Context, filter interface{}, update interface{}) (result *qmgo.UpdateResult, err error)
UpdateAll executes an update command to update documents in the collection. The matchedCount is 0 in UpdateResult if no document updated Reference: https://docs.mongodb.com/manual/reference/operator/update/
func (*Collection) UpdateId ¶
func (c *Collection) UpdateId(ctx context.Context, id interface{}, update interface{}) (err error)
UpdateId executes an update command to update at most one document in the collection. Reference: https://docs.mongodb.com/manual/reference/operator/update/
func (*Collection) UpdateOne ¶
func (c *Collection) UpdateOne(ctx context.Context, filter interface{}, update interface{}) (err error)
UpdateOne executes an update command to update at most one document in the collection. Reference: https://docs.mongodb.com/manual/reference/operator/update/
func (*Collection) Upsert ¶
func (c *Collection) Upsert(ctx context.Context, filter interface{}, replacement interface{}) (result *qmgo.UpdateResult, err error)
Upsert updates one documents if filter match, inserts one document if filter is not match, Error when the filter is invalid The replacement parameter must be a document that will be used to replace the selected document. It cannot be nil and cannot contain any update operators Reference: https://docs.mongodb.com/manual/reference/operator/update/ If replacement has "_id" field and the document is exist, please initial it with existing id(even with Qmgo default field feature). Otherwise "the (immutable) field '_id' altered" error happens.
func (*Collection) UpsertId ¶
func (c *Collection) UpsertId(ctx context.Context, id interface{}, replacement interface{}) (result *qmgo.UpdateResult, err error)
UpsertId updates one documents if id match, inserts one document if id is not match and the id will inject into the document The replacement parameter must be a document that will be used to replace the selected document. It cannot be nil and cannot contain any update operators Reference: https://docs.mongodb.com/manual/reference/operator/update/
type Config ¶
type Config struct { DSN string DBName string Addr string MaxPoolSize uint64 MinPoolSize uint64 SlowQueryDuration time.Duration }
Config MongoDB DSN configs
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB encapsulation of qmgo client and database
func (*DB) GetCollection ¶
func (d *DB) GetCollection(name string) *Collection
GetCollection return collection handler
type ObjectID ¶ added in v1.3.3
ObjectID is an alias of primitive.ObjectID
func ObjectIDFromHex ¶ added in v1.3.3
ObjectIDFromHex creates a new ObjectID from a hex string It returns an error if the hex string is not a valid ObjectID
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query struct definition
func (*Query) All ¶
All query multiple records that meet the filter conditions The static type of result must be a slice pointer
func (*Query) Apply ¶
Apply runs the findAndModify command, which allows updating, replacing or removing a document matching a query and atomically returning either the old version (the default) or the new version of the document (when ReturnNew is true)
The Sort and Select query methods affect the result of Apply. In case multiple documents match the query, Sort enables selecting which document to act upon by ordering it first. Select enables retrieving only a selection of fields of the new or old document.
When Change.Replace is true, it means replace at most one document in the collection and the update parameter must be a document and cannot contain any update operators; if no objects are found and Change.Upsert is false, it will returns ErrNoDocuments. When Change.Remove is true, it means delete at most one document in the collection and returns the document as it appeared before deletion; if no objects are found, it will returns ErrNoDocuments. When both Change.Replace and Change.Remove are false,it means update at most one document in the collection and the update parameter must be a document containing update operators; if no objects are found and Change.Upsert is false, it will returns ErrNoDocuments.
reference: https://docs.mongodb.com/manual/reference/command/findAndModify/
func (*Query) Cursor ¶
Cursor gets a Cursor object, which can be used to traverse the query result set After obtaining the CursorI object, you should actively call the Close interface to close the cursor Strongly suggest use One or All
func (*Query) Distinct ¶
Distinct gets the unique value of the specified field in the collection and return it in the form of slice result should be passed a pointer to slice The function will verify whether the static type of the elements in the result slice is consistent with the data type obtained in mongodb reference https://docs.mongodb.com/manual/reference/command/distinct/
func (*Query) Hint ¶
Hint sets the value for the Hint field. This should either be the index name as a string or the index specification as a document. The default value is nil, which means that no hint will be sent.
func (*Query) Limit ¶
Limit limits the maximum number of documents found to n The default value is 0, and 0 means no limit, and all matching results are returned When the limit value is less than 0, the negative limit is similar to the positive limit, but the cursor is closed after returning a single batch result. Reference https://docs.mongodb.com/manual/reference/method/cursor.limit/index.html
func (*Query) One ¶
One query a record that meets the filter conditions If the search fails, an error will be returned
func (*Query) Select ¶
Select is used to determine which fields are displayed or not displayed in the returned results Format: bson.M{"age": 1} means that only the age field is displayed bson.M{"age": 0} means to display other fields except age When _id is not displayed and is set to 0, it will be returned to display
func (*Query) Sort ¶
Sort is Used to set the sorting rules for the returned results Format: "age" or "+age" means to sort the age field in ascending order, "-age" means in descending order When multiple sort fields are passed in at the same time, they are arranged in the order in which the fields are passed in. For example, {"age", "-name"}, first sort by age in ascending order, then sort by name in descending order