mongo

package
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitClients

func InitClients()

func StringsToBsonId

func StringsToBsonId(ids []string) (objectIds []primitive.ObjectID)

Types

type BulkUpdateItem

type BulkUpdateItem struct {
	Selector bson.M
	Update   bson.M
	IsMulti  bool
}

type BulkUpsertItem added in v1.0.17

type BulkUpsertItem struct {
	Selector    bson.M
	Replacement interface{}
}

type Client

type Client struct {
	*mongo.Client
}

func MgoClient

func MgoClient(key string) (mongo *Client, err error)

func (*Client) Aggregate

func (client *Client) Aggregate(ctx context.Context, collection *mongo.Collection, pipeline interface{}, result interface{},
	opts ...*options.AggregateOptions) error

func (*Client) BatchInsert

func (client *Client) BatchInsert(ctx context.Context, collection *mongo.Collection, data []interface{},
	opts ...*options.InsertManyOptions) error

func (*Client) BulkUpdate

func (client *Client) BulkUpdate(ctx context.Context, collection *mongo.Collection, bulkModels []mongo.WriteModel,
	opts ...*options.BulkWriteOptions) error

func (*Client) BulkUpdateItems

func (client *Client) BulkUpdateItems(ctx context.Context, collection *mongo.Collection, bulkUpdateItems []*BulkUpdateItem,
	opts ...*options.BulkWriteOptions) error

func (*Client) BulkUpsertItems

func (client *Client) BulkUpsertItems(ctx context.Context, collection *mongo.Collection, bulkUpsertItems []*BulkUpsertItem,
	opts ...*options.BulkWriteOptions) error

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

func (*Client) Count

func (client *Client) Count(ctx context.Context, collection *mongo.Collection, query interface{},
	opts ...*options.CountOptions) (int64, error)

func (*Client) Delete

func (client *Client) Delete(ctx context.Context, collection *mongo.Collection, selector interface{},
	opts ...*options.DeleteOptions) error

func (*Client) DeleteById

func (client *Client) DeleteById(ctx context.Context, collection *mongo.Collection, id primitive.ObjectID,
	opts ...*options.DeleteOptions) error

func (*Client) ExecTx

func (client *Client) ExecTx(ctx context.Context, f func(context.Context)) error

需要mongodb4.2以上才能支持事务

func (*Client) FindById

func (client *Client) FindById(ctx context.Context, collection *mongo.Collection, id primitive.ObjectID, result interface{},
	opts ...*options.FindOneOptions) error

read

func (*Client) FindByIds

func (client *Client) FindByIds(ctx context.Context, collection *mongo.Collection, ids []primitive.ObjectID, result interface{},
	opts ...*options.FindOptions) error

func (*Client) FindByModel

func (client *Client) FindByModel(ctx context.Context, collection *mongo.Collection, model FindModel,
	opts ...*options.FindOptions) error

func (*Client) FindOneAndUpdate

func (client *Client) FindOneAndUpdate(ctx context.Context, collection *mongo.Collection, result, selector, updateMap interface{},
	returnNew bool, upsert bool) error

func (*Client) FindOneByModel

func (client *Client) FindOneByModel(ctx context.Context, collection *mongo.Collection, model FindModel,
	opts ...*options.FindOneOptions) error

func (*Client) Insert

func (client *Client) Insert(ctx context.Context, collection *mongo.Collection, data interface{},
	opts ...*options.InsertOneOptions) error

write

func (*Client) Update

func (client *Client) Update(ctx context.Context, collection *mongo.Collection, selector interface{}, update interface{},
	opts ...*options.UpdateOptions) error

func (*Client) UpdateById

func (client *Client) UpdateById(ctx context.Context, collection *mongo.Collection, id primitive.ObjectID, update interface{},
	opts ...*options.UpdateOptions) error

func (*Client) UpdateByIds

func (client *Client) UpdateByIds(ctx context.Context, collection *mongo.Collection, ids []primitive.ObjectID, update interface{},
	opts ...*options.UpdateOptions) error

func (*Client) Upsert

func (client *Client) Upsert(ctx context.Context, collection *mongo.Collection, selector interface{}, update interface{},
	opts ...*options.UpdateOptions) error

type CollectionOp

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

集合操作类

func NewMgoCollection

func NewMgoCollection(client *Client, dbName, collectionName string,
	dbOpts []*options.DatabaseOptions, cOpts []*options.CollectionOptions) *CollectionOp

func (*CollectionOp) Aggregate

func (c *CollectionOp) Aggregate(ctx context.Context, pipeline interface{}, result interface{}) error

func (*CollectionOp) BatchInsert

func (c *CollectionOp) BatchInsert(ctx context.Context, data []interface{}) error

func (*CollectionOp) BulkUpdateItems added in v1.0.17

func (c *CollectionOp) BulkUpdateItems(ctx context.Context, bulkUpdateItems []*BulkUpdateItem,
	opts ...*options.BulkWriteOptions) error

func (*CollectionOp) BulkUpsertItem added in v1.0.17

func (c *CollectionOp) BulkUpsertItem(ctx context.Context, bulkUpertItems []*BulkUpsertItem,
	opts ...*options.BulkWriteOptions) error

func (*CollectionOp) Count

func (c *CollectionOp) Count(ctx context.Context, query interface{}) (int64, error)

func (*CollectionOp) Delete

func (c *CollectionOp) Delete(ctx context.Context, selector interface{}) error

func (*CollectionOp) DeleteById

func (c *CollectionOp) DeleteById(ctx context.Context, id primitive.ObjectID) error

func (*CollectionOp) Find

func (c *CollectionOp) Find(ctx context.Context, model FindModel, opts ...*options.FindOptions) error

func (*CollectionOp) FindById

func (c *CollectionOp) FindById(ctx context.Context, id primitive.ObjectID, result interface{}) error

func (*CollectionOp) FindOne

func (c *CollectionOp) FindOne(ctx context.Context, model FindModel, opts ...*options.FindOneOptions) error

func (*CollectionOp) FindOneAndUpdate

func (c *CollectionOp) FindOneAndUpdate(ctx context.Context, result interface{}, query interface{}, updateMap interface{},
	returnNew bool, upsert bool) error

func (*CollectionOp) Insert

func (c *CollectionOp) Insert(ctx context.Context, data interface{}) error

func (*CollectionOp) Update

func (c *CollectionOp) Update(ctx context.Context, selector interface{}, updateOp interface{}) error

func (*CollectionOp) UpdateById

func (c *CollectionOp) UpdateById(ctx context.Context, id primitive.ObjectID, updateOp interface{}) error

func (*CollectionOp) Upsert

func (c *CollectionOp) Upsert(ctx context.Context, selector interface{}, updateOp bson.M, setOnInsertItem interface{}) error

setOnInsertM only write on insert

type CompCollectionOp

type CompCollectionOp struct {
	*CollectionOp               //全局配置
	Primary       *CollectionOp //强制读主库
	Slave         *CollectionOp //优先读从库
}

复合集合操作类

func NewCompCollectionOp

func NewCompCollectionOp(client *Client, dbName, collectionName string) *CompCollectionOp

type FindModel

type FindModel struct {
	Query   interface{}
	Fields  interface{}
	Sort    []string
	Cursor  int
	Size    int
	Results interface{}
}

Jump to

Keyboard shortcuts

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