mongo

package
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 IsDup added in v1.3.0

func IsDup(err error) bool

IsDup check if err is mongo E11000 (duplicate err)

func IsErrNoDocuments added in v1.3.0

func IsErrNoDocuments(err error) bool

IsErrNoDocuments check if err is no documents, simply call if err == ErrNoSuchDocuments or if err == mongo.ErrNoDocuments

Types

type A added in v1.3.0

type A = bson.A

A is an alias of bson.A

type Aggregate

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

func (*Aggregate) All

func (a *Aggregate) All(results interface{}) (err error)

All iterates the cursor from aggregate and decodes each document into results.

func (*Aggregate) Iter

func (a *Aggregate) Iter() qmgo.CursorI

Iter return the cursor after aggregate In most scenario do not use Iter

func (*Aggregate) One

func (a *Aggregate) One(result interface{}) (err error)

One iterates the cursor from aggregate and decodes current document into result.

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) InsertOne

func (b *Bulk) InsertOne(doc interface{}) *Bulk

InsertOne queues an InsertOne operation for bulk execution.

func (*Bulk) Remove

func (b *Bulk) Remove(filter interface{}) *Bulk

Remove queues a Remove operation for bulk execution.

func (*Bulk) RemoveAll

func (b *Bulk) RemoveAll(filter interface{}) *Bulk

RemoveAll queues a RemoveAll operation for bulk execution.

func (*Bulk) RemoveId

func (b *Bulk) RemoveId(id interface{}) *Bulk

RemoveId queues a RemoveId operation for bulk execution.

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

func (b *Bulk) SetOrdered(ordered bool) *Bulk

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

func (b *Bulk) UpdateAll(filter interface{}, update interface{}) *Bulk

UpdateAll queues an UpdateAll operation for bulk execution. The update should contain operator

func (*Bulk) UpdateId

func (b *Bulk) UpdateId(id interface{}, update interface{}) *Bulk

UpdateId queues an UpdateId operation for bulk execution. The update should contain operator

func (*Bulk) UpdateOne

func (b *Bulk) UpdateOne(filter interface{}, update interface{}) *Bulk

UpdateOne queues an UpdateOne operation for bulk execution. The update should contain operator

func (*Bulk) Upsert

func (b *Bulk) Upsert(filter interface{}, replacement interface{}) *Bulk

Upsert queues an Upsert operation for bulk execution. The replacement should be document without operator

func (*Bulk) UpsertId

func (b *Bulk) UpsertId(id interface{}, replacement interface{}) *Bulk

UpsertId queues an UpsertId operation for bulk execution. The replacement should be document without 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 D added in v1.3.0

type D = bson.D

D is an alias of bson.D

type DB

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

DB encapsulation of qmgo client and database

func Open

func Open(config *Config) *DB

Open return database instance handler

func (*DB) Close

func (d *DB) Close() error

Close close the db connection

func (*DB) GetCollection

func (d *DB) GetCollection(name string) *Collection

GetCollection return collection handler

func (*DB) Ping

func (d *DB) Ping() error

Ping ping mongo to keepalive

type E added in v1.3.0

type E = bson.E

E is an alias of bson.E

type M added in v1.3.0

type M = bson.M

M is an alias of bson.M

type ObjectID added in v1.3.3

type ObjectID = primitive.ObjectID

ObjectID is an alias of primitive.ObjectID

func NewObjectID added in v1.3.3

func NewObjectID() ObjectID

NewObjectID generates a new ObjectID

func ObjectIDFromHex added in v1.3.3

func ObjectIDFromHex(s string) (ObjectID, error)

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

func (q *Query) All(result interface{}) (err error)

All query multiple records that meet the filter conditions The static type of result must be a slice pointer

func (*Query) Apply

func (q *Query) Apply(change qmgo.Change, result interface{}) error

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) Count

func (q *Query) Count() (n int64, err error)

Count count the number of eligible entries

func (*Query) Cursor

func (q *Query) Cursor() qmgo.CursorI

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

func (q *Query) Distinct(key string, result interface{}) (err error)

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

func (q *Query) Hint(hint interface{}) *Query

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

func (q *Query) Limit(n int64) *Query

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

func (q *Query) One(result interface{}) (err error)

One query a record that meets the filter conditions If the search fails, an error will be returned

func (*Query) Select

func (q *Query) Select(projection interface{}) *Query

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) Skip

func (q *Query) Skip(n int64) *Query

Skip skip n records

func (*Query) Sort

func (q *Query) Sort(fields ...string) *Query

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

Jump to

Keyboard shortcuts

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