mgo

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildWhere

func BuildWhere(Filter []string) [][]interface{}

BuildWhere 构造Where搜索

func Close added in v1.5.2

func Close()

Close the connection<关闭连接>

func GetConfig

func GetConfig() (string, string, uint64, int)

GetConfig 获取配置

func InitEngine added in v1.5.2

func InitEngine(databaseName ...string) error

InitEngine 初始化 参考文档 https://docs.mongodb.com/drivers/go/

Types

type Mgo

type Mgo struct {
	Database     *mongo.Database //数据库
	Ctx          context.Context
	Collection   *mongo.Collection
	Cancel       context.CancelFunc
	DataBaseName string
	TableName    string
	Filter       interface{}
	Pages        Pages
	Timeout      int
	Err          error
	sync.Mutex
}

Mgo

func NewMongoDB added in v1.5.2

func NewMongoDB(tableName string, databaseName ...string) *Mgo

Connect Default connection<默认连接> 参考文档 https://docs.mongodb.com/drivers/go/

func (*Mgo) Aggregate

func (m *Mgo) Aggregate() (*mongo.Cursor, error)

Aggregate Aggregate advanced queries<聚合高级查询> id, _ := primitive.ObjectIDFromHex("5e3b37e51c9d4400004117e6") matchStage := bson.D{{"$match", bson.D{{"podcast", id}}}} groupStage := bson.D{{"$group", bson.D{{"_id", "$podcast"}, {"total", bson.D{{"$sum", "$duration"}}}}}} mongo.Pipeline{matchStage, groupStage}

func (*Mgo) Count

func (m *Mgo) Count(count *int64) *Mgo

Count Get the total quantity<获取总数量>

func (*Mgo) Create

func (m *Mgo) Create(data interface{}) (*mongo.InsertOneResult, error)

Create Create data<创建数据>

func (*Mgo) Delete

func (m *Mgo) Delete(update interface{}) (*mongo.DeleteResult, error)

Delete delete data<删除>

func (*Mgo) DeleteMany

func (m *Mgo) DeleteMany(update interface{}) (*mongo.DeleteResult, error)

DeleteMany Delete multiple data<删除多个数据>

func (*Mgo) Distinct

func (m *Mgo) Distinct(name string) ([]interface{}, error)

Distinct Query unique data<查询不重复的数据>

func (*Mgo) Find

func (m *Mgo) Find() (*mongo.Cursor, error)

Find Multiple data search<多条数据查询>

func (*Mgo) FindOne

func (m *Mgo) FindOne(v interface{}) error

FindOne Single query<单个查询>

func (*Mgo) GetCtx

func (m *Mgo) GetCtx() context.Context

GetCtx Get context<获取上下文>

func (*Mgo) Limit

func (m *Mgo) Limit(limit int64) *Mgo

Limit Limited number<显示数量>

func (*Mgo) SaveAll

func (m *Mgo) SaveAll(data []interface{}) (*mongo.InsertManyResult, error)

SaveAll save all data<创建多条数据>

func (*Mgo) SetDatabase

func (m *Mgo) SetDatabase(databaseName string) *Mgo

SetDatabase Modify database switch<修改数据库切换>

func (*Mgo) Skip

func (m *Mgo) Skip(skip int64) *Mgo

Skip How many pages to jump<跳转多少页>

func (*Mgo) Sort

func (m *Mgo) Sort(sort map[string]interface{}) *Mgo

Sort Sort data<排序> {KEY:1},{KEY:-1}

func (*Mgo) StartTrans

func (m *Mgo) StartTrans(fn func(mongo.SessionContext) error)

StartTrans 开启事务 sessionContext.StartTransaction() 开启事务 sessionContext.AbortTransaction(sessionContext) //终止事务 sessionContext.CommitTransaction(sessionContext) //提交事务

func (*Mgo) Table

func (m *Mgo) Table(tableName string) *Mgo

Table Setting table<表>

func (*Mgo) Update

func (m *Mgo) Update(filter interface{}, update interface{}) (*mongo.UpdateResult, error)

Update Update data<修改数据>

func (*Mgo) UpdateById

func (m *Mgo) UpdateById(id string, update interface{}) (*mongo.UpdateResult, error)

UpdateById Modify data according to id<根据id修改>

func (*Mgo) Where

func (m *Mgo) Where(filter interface{}) *Mgo

Where 条件 [][]string{{"test", "name"},{"test", "$lt","1"}}

map[string]interface{}{
		"$or": []interface{}{map[string]interface{}{"author": "Nicolas222"}, map[string]interface{}{"author": "Nicolas333"}},
	}

等于 {<key>:<value>} 小于 {<key>:{$lt:<value>}} 小于或等于 {<key>:{$lte:<value>}} 大于 {<key>:{$gt:<value>}} 大于或等于 {<key>:{$gte:<value>}} 不等于 {<key>:{$ne:<value>}}

type Pages

type Pages struct {
	Limit *int64
	Skip  *int64
	Sort  interface{}
}

Pages 分页过滤排序

Jump to

Keyboard shortcuts

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