mgoDB

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2020 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMgoClient

func GetMgoClient(config map[string]string) (*mongo.Client, error)

获取mongodb连接

Types

type CollectionContent

type CollectionContent struct {
	Database       *mongo.Database
	DatabaseName   string
	Collection     *mongo.Collection
	CollectionName string
}

集合上下文

func (*CollectionContent) IsCreateIndex

func (mgo *CollectionContent) IsCreateIndex() (c *CollectionContent, isCreate bool)

是否创建索引

func (*CollectionContent) SetIndex

func (mgo *CollectionContent) SetIndex(elems []string) *CollectionContent

@elems 索引集合(正序)

func (*CollectionContent) SetIndexDesc

func (mgo *CollectionContent) SetIndexDesc(elems []string) *CollectionContent

@elems 索引集合(倒序)

func (*CollectionContent) SetUnique

func (mgo *CollectionContent) SetUnique(elems []string) *CollectionContent

@elems 索引集合

func (*CollectionContent) SetUniqueDesc

func (mgo *CollectionContent) SetUniqueDesc(elems []string) *CollectionContent

@elems 索引集合(倒序)

type IMgoRepository

type IMgoRepository interface {
	// 插入单个
	// entity  结构体
	Insert(entity interface{}) (count int, err error)
	// 插入多个
	// entity 结构体切片
	InsertMany(entity []interface{}) (count int, err error)
	// 删除单个
	// id 主键
	Delete(id interface{}) (count int, err error)
	// 删除多个
	// ids 主键切片
	DeleteMany(ids ...interface{}) (count int, err error)
	// filterKey   过滤的字段名称[与标签一致]
	// filterValue 过滤的字段值
	// entity      更新的结构体
	Update(filterKey string, filterValue interface{}, entity interface{}) (count int, err error)
	// 单条件查询单个[id]
	// filterValue 过滤的字段值
	// result      查询的结果(指针)
	FindById(filterValue interface{}, result interface{}) (err error)
	// 单条件查询
	// filterKey   过滤的字段名称[与标签一致]
	// filterValue 过滤的字段值
	// result      查询的结果
	Find(filterKey string, filterValue interface{}, result interface{}) (err error)
	// 多条件查询
	// filterMap   过滤的条件集合[与标签一致]
	// result      查询的结果
	FindMany(filterMap map[string]interface{}, result interface{}) (err error)
	// 获取总数
	// filterMap   过滤的条件集合
	FindCount(filterMap map[string]interface{}) (count int64, err error)
	// 查询分页
	// filterMap 查询条件[与标签一致]
	// pageIndex  页数(从1开始)
	// size 获取个数
	// sortName 排序字段
	// desc 是否倒序(1为正序,-1为倒序) 1 为最初时间读取 , -1 为最新时间读取
	// result 查询结果(结构体切片)
	// isTotal 总数
	FindPage(filterMap map[string]interface{}, pageIndex, size int64, sortName string, desc int, result interface{}, isTotal bool) (totalCount int64, err error)
}

mongodb 仓储接口

type MgoContent

type MgoContent struct {
	Database     *mongo.Database
	DatabaseName string
}

MgoContent 数据库上下文

func DataBaseMapping

func DataBaseMapping(client *mongo.Client, dataName string) *MgoContent

添加映射 @client 连接对象 @dataName 数据库名

func (*MgoContent) GetCollection

func (mgo *MgoContent) GetCollection(entity interface{}) *CollectionContent

获取集合

func (*MgoContent) GetMgoRepository

func (mgo *MgoContent) GetMgoRepository(entity interface{}) IMgoRepository

获取集合仓储 entity 结构体

type MgoRepository

type MgoRepository struct {
	*CollectionContent
}

MgoRepository 数据库仓储

func (*MgoRepository) Delete

func (mgo *MgoRepository) Delete(id interface{}) (count int, err error)

删除单个 id 主键

func (*MgoRepository) DeleteMany

func (mgo *MgoRepository) DeleteMany(ids ...interface{}) (count int, err error)

删除单个 id 主键切片

func (*MgoRepository) Find

func (mgo *MgoRepository) Find(filterKey string, filterValue interface{}, result interface{}) (err error)

单条件查询单个 filterKey 过滤的字段名称 filterValue 过滤的字段值 result 查询的结果(指针)

func (*MgoRepository) FindById

func (mgo *MgoRepository) FindById(filterValue interface{}, result interface{}) (err error)

单条件查询单个[id] filterValue 过滤的字段值 result 查询的结果(指针)

func (*MgoRepository) FindCount

func (mgo *MgoRepository) FindCount(filterMap map[string]interface{}) (count int64, err error)

查询总数 filterMap 查询条件

func (*MgoRepository) FindMany

func (mgo *MgoRepository) FindMany(filterMap map[string]interface{}, result interface{}) (err error)

多条件查询 filterMap 过滤的条件集合 result 查询的结果切片(指针)

func (*MgoRepository) FindPage

func (mgo *MgoRepository) FindPage(filterMap map[string]interface{}, pageIndex, size int64, sortName string, desc int, result interface{}, isTotal bool) (totalCount int64, err error)

查询分页 filterMap 查询条件

	模糊查询:map["name"] = primitive.Regex{Pattern: "深入"}
 查询createtime>=3: bson.M{"createtime": bson.M{"$gte": 2}}
 二级结构体查询: map["author.country"] = countryChina

pageIndex 页数(从1开始) size 获取个数 sortName 排序字段 desc 是否倒序(1为正序,-1为倒序) 1 为最初时间读取 , -1 为最新时间读取 result 查询结果(结构体切片) isTotal 总数

func (*MgoRepository) Insert

func (mgo *MgoRepository) Insert(entity interface{}) (count int, err error)

插入单个 entity 结构体

func (*MgoRepository) InsertMany

func (mgo *MgoRepository) InsertMany(entity []interface{}) (count int, err error)

插入多个 entity 结构体切片

func (*MgoRepository) Update

func (mgo *MgoRepository) Update(filterKey string, filterValue interface{}, entity interface{}) (count int, err error)

filterKey 过滤的字段名称 filterValue 过滤的字段值 entity 更新的结构体

Jump to

Keyboard shortcuts

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