Documentation ¶
Index ¶
- Variables
- type Client
- func (c *Client) Connect(ctxs ...context.Context) error
- func (c *Client) Database(name string, opts ...*options.DatabaseOptions) *Database
- func (c *Client) Disconnect(ctxs ...context.Context) error
- func (c *Client) MongoClient() *mongo.Client
- func (c *Client) StartSession(opts ...*options.SessionOptions) (mongo.Session, error)
- type ClientOptions
- type Collection
- func (coll *Collection) Client() *Client
- func (coll *Collection) Count(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int, error)
- func (coll *Collection) Database() *Database
- func (coll *Collection) Delete(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (int, error)
- func (coll *Collection) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (int, error)
- func (coll *Collection) Find(ctx context.Context, result interface{}, filter interface{}, ...) error
- func (coll *Collection) FindOne(ctx context.Context, result interface{}, filter interface{}, ...) error
- func (coll *Collection) Insert(ctx context.Context, documents []interface{}, ...) ([]types.ObjectID, error)
- func (coll *Collection) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (types.ObjectID, error)
- func (coll *Collection) MongoCollection() *mongo.Collection
- func (coll *Collection) Name() string
- func (coll *Collection) Save(ctx context.Context, filter interface{}, document interface{}, ...) (types.ObjectID, error)
- func (coll *Collection) Update(ctx context.Context, filter interface{}, update interface{}, ...) (*mongo.UpdateResult, error)
- func (coll *Collection) UpdateOne(ctx context.Context, filter interface{}, update interface{}, ...) (*mongo.UpdateResult, error)
- type Database
- type Transaction
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrDocumentExists = errors.New("mongodb: document already exists")
View Source
var ErrFilterNil = errors.New("mongodb: filter cannot be nil")
View Source
var ErrResultNil = errors.New("mongodb: the result point cannot be nil")
View Source
var ErrResultSlice = errors.New("mongodb: result slice type conversion failure")
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func Connect ¶
func Connect(opts *ClientOptions) (*Client, error)
func NewClient ¶
func NewClient(opts *ClientOptions) (*Client, error)
func (*Client) Database ¶
func (c *Client) Database(name string, opts ...*options.DatabaseOptions) *Database
func (*Client) MongoClient ¶
func (*Client) StartSession ¶
type ClientOptions ¶
type ClientOptions struct { //custom options URI string Timeout time.Duration //options.ClientOptions MaxPoolSize uint64 MinPoolSize uint64 // contains filtered or unexported fields }
func NewClientOptions ¶
func NewClientOptions(uri string) *ClientOptions
func NewClientOptionsFromConfig ¶
func NewClientOptionsFromConfig(conf *config.MongodbConfig) *ClientOptions
func NewClientOptionsFromFile ¶ added in v0.0.3
func NewClientOptionsFromFile(files ...string) *ClientOptions
func (*ClientOptions) Apply ¶
func (c *ClientOptions) Apply()
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
func (*Collection) Client ¶
func (coll *Collection) Client() *Client
func (*Collection) Count ¶
func (coll *Collection) Count(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int, error)
统计匹配的记录数量
func (*Collection) Database ¶
func (coll *Collection) Database() *Database
func (*Collection) Delete ¶
func (coll *Collection) Delete(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (int, error)
删除所有匹配的记录, 返回删除数量
func (*Collection) DeleteOne ¶
func (coll *Collection) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (int, error)
删除匹配的第一条记录, 返回删除数量
func (*Collection) Find ¶
func (coll *Collection) Find(ctx context.Context, result interface{}, filter interface{}, opts ...*options.FindOptions) error
查找匹配的所有记录, result 为存储结果的对象指针 var users []*model.User 或 users := make([]*model.User, 0) 或 users := []*model.User{} 然后 err := Find(&users, collUser, nil) users := make([]interface{}, 0) 错误,不能是interface{}万能类型,必须是确定的类型
func (*Collection) FindOne ¶
func (coll *Collection) FindOne(ctx context.Context, result interface{}, filter interface{}, opts ...*options.FindOneOptions) error
查找匹配的第一条记录, result 为存储结果的对象指针
func (*Collection) Insert ¶
func (coll *Collection) Insert(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) ([]types.ObjectID, error)
插入多条记录,返回 []ObjectID
func (*Collection) InsertOne ¶
func (coll *Collection) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (types.ObjectID, error)
插入一条记录, 返回 ObjectID
func (*Collection) MongoCollection ¶
func (coll *Collection) MongoCollection() *mongo.Collection
func (*Collection) Name ¶
func (coll *Collection) Name() string
func (*Collection) Save ¶
func (coll *Collection) Save(ctx context.Context, filter interface{}, document interface{}, opts ...*options.InsertOneOptions) (types.ObjectID, error)
查找一条记录,如果不存在,则插入一条记录
func (*Collection) Update ¶
func (coll *Collection) Update(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
修改匹配的所有记录, 返回修改信息
func (*Collection) UpdateOne ¶
func (coll *Collection) UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
修改匹配的第一条记录, 返回修改信息
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
func (*Database) Collection ¶
func (db *Database) Collection(name string, opts ...*options.CollectionOptions) *Collection
func (*Database) MongoDatabase ¶
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
func NewTransaction ¶
func NewTransaction(client *Client) *Transaction
func (*Transaction) Commit ¶
func (t *Transaction) Commit() error
func (*Transaction) Rollback ¶
func (t *Transaction) Rollback() error
func (*Transaction) Run ¶
func (t *Transaction) Run(fn func(sctx mongo.SessionContext) error) error
自动执行事务,简化过程
Source Files ¶
Click to show internal directories.
Click to hide internal directories.