mongoose

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

mongoose

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CollectionNameNotFound = errors.New("when filter is not IDocument, then need CollectionName option")
	InvalidDocument        = errors.New("invalid document impl IDocument interface at least")
)

Functions

func CombAndFilters

func CombAndFilters(filters ...interface{}) bson.M

func CombOrFilters

func CombOrFilters(filters ...interface{}) bson.M

func CombineFilters

func CombineFilters(operator string, filters ...interface{}) bson.M

CombineFilters 用来组合多个bson.M filter operator : $and $or ...

func ConvertFilter

func ConvertFilter(v interface{}, fatherTag string) bson.M

ConvertFilter convert Struct or Ptr to bson.M

func ConvertId

func ConvertId(id interface{}) (oid primitive.ObjectID, err error)

func Eq

func Eq(field string, value interface{}) bson.M

func Gt

func Gt(field string, value interface{}) bson.M

func Gte

func Gte(field string, value interface{}) bson.M

func IdFilter

func IdFilter(id primitive.ObjectID) bson.M

func In

func In(field string, value interface{}) bson.M

func Lt

func Lt(field string, value interface{}) bson.M

func Lte

func Lte(field string, value interface{}) bson.M

func Ne

func Ne(field string, value interface{}) bson.M

func Nin

func Nin(field string, value interface{}) bson.M

func ParseFilter

func ParseFilter(filter interface{}) interface{}

ParseFilter 检查filter并进行转化. 转化为 driver 支持的格式 如果本身就是 bson.M 或 bson.D 系列则不进行转化 如果是结构体或者结构体指针则进行转化 (支持复杂结构体)

func Set

func Set(doc interface{}) bson.M

func SimpleStructToDoc

func SimpleStructToDoc(v interface{}) (doc *bsoncore.Document, err error)

SimpleStructToDoc 没有嵌套的结构体可以使用此方法进行转化

func UnDeletedFilterByID

func UnDeletedFilterByID(id primitive.ObjectID) bson.M

func UndeleteFilter

func UndeleteFilter() bson.M

Types

type DeleteOption

type DeleteOption struct {
	CollectionName string
	DriverOptions  []*options.DeleteOptions
}

type Document

type Document struct {
	ID        primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
	CreatedAt time.Time          `bson:"created_at,omitempty" json:"created_at,omitempty"`
	UpdatedAt time.Time          `bson:"updated_at,omitempty" json:"updated_at,omitempty"`
	DeletedAt time.Time          `bson:"deleted_at,omitempty" json:"-"`
}

func (*Document) PreCreate

func (d *Document) PreCreate()

func (*Document) PreDelete

func (d *Document) PreDelete()

func (*Document) PreUpdate

func (d *Document) PreUpdate()

type IDocument

type IDocument interface {
	PreCreate()
	PreUpdate()
	PreDelete()
	CollectionName() string
}

type Mongo

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

func Open

func Open(opt *Options) (*Mongo, error)

func (*Mongo) DeleteMany

func (m *Mongo) DeleteMany(filter interface{}, opt *DeleteOption) (*mongo.DeleteResult, error)

DeleteMany 删除多条数据

func (*Mongo) DeleteOne

func (m *Mongo) DeleteOne(filter interface{}, opt *DeleteOption) (*mongo.DeleteResult, error)

DeleteOne 删除一条数据 如果 filter 是IDocument 类型 那么会自动找到 collection 否则需要 指明 opt.CollectionName 参数

func (*Mongo) Find

func (m *Mongo) Find(filter interface{}, results interface{}, opts ...*options.FindOptions) (err error)

Find 基础查找 filter 支持 bson 以及 IDocument 如果filter 是一个 Document 那么他必须是 addressable 的, 也就是说是一个指针. result 则是一个 存储结果的指针 例如 &[]SomeDoc or make([]SomeDoc,0)

func (*Mongo) FindByID

func (m *Mongo) FindByID(id interface{}, result interface{}, opts ...*options.FindOneOptions) (err error)

FindByID 通过id查找数据

func (*Mongo) FindOne

func (m *Mongo) FindOne(filter interface{}, result interface{}, opts ...*options.FindOneOptions) (err error)

FindOne 查找一条数据 filter 支持 bson 以及 IDocument result 则是一个 存储结果的指针 例如 &SomeDoc

func (*Mongo) FindOneUndeleteByFilter

func (m *Mongo) FindOneUndeleteByFilter(filter interface{}, result interface{}, opts ...*options.FindOneOptions) (err error)

FindOneUndeleteByFilter 查找一条未删除的数据

func (*Mongo) FindUnDeleteByID

func (m *Mongo) FindUnDeleteByID(id interface{}, result interface{}, opts ...*options.FindOneOptions) (err error)

FindUnDeleteByID 查找一条未删除的数据

func (*Mongo) FindUndeleteByFilter

func (m *Mongo) FindUndeleteByFilter(filter interface{}, results interface{}, opts ...*options.FindOptions) (err error)

FindUndeleteByFilter 查找未删除的所有数据

func (*Mongo) GetCollectionWithName

func (m *Mongo) GetCollectionWithName(name string, opts ...*options.CollectionOptions) *mongo.Collection

GetCollectionWithName 提供原始查询方法

func (*Mongo) InsertMany

func (m *Mongo) InsertMany(docs []IDocument, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)

InsertMany 插入多条数据

func (*Mongo) InsertOne

func (m *Mongo) InsertOne(doc IDocument, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)

InsertOne 插入一条数据

func (*Mongo) Release added in v0.1.2

func (m *Mongo) Release(ctx context.Context) error

func (*Mongo) UpdateByID

func (m *Mongo) UpdateByID(id interface{}, update IDocument, opts ...*options.UpdateOptions) (*mongo.UpdateResult,
	error)

UpdateByID 通过ID更新 支持 string 或 objectId

func (*Mongo) UpdateMany

func (m *Mongo) UpdateMany(filter interface{}, update IDocument, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

UpdateMany 更新多条数据

func (*Mongo) UpdateOne

func (m *Mongo) UpdateOne(filter interface{}, update IDocument, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

UpdateOne 更新一条数据

type Options added in v0.1.1

type Options struct {
	User     string
	Password string
	Host     string
	Port     int
	DBName   string

	ConnectTimeout         time.Duration
	ServerSelectionTimeout time.Duration

	DriverOpts []*options.ClientOptions
}

Jump to

Keyboard shortcuts

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