mongoose

package module
v0.2.7 Latest Latest
Warning

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

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

README

mongoose document

Example

package main

import (
	"context"
	"log"
	"github.com/wuruipeng404/mongoose"
	"go.mongodb.org/mongo-driver/bson"
)

var odm *mongoose.Mongo

// get mongo client
func init() {

	var err error

	if odm, err = mongoose.Open(&mongoose.Options{
		User:       "user",
		Password:   "password",
		Host:       "localhost",
		Port:       27017,
		DBName:     "your-db",
		DriverOpts: nil, // and you can add driver client options
	}); err != nil {
		log.Fatalf("connect mongoose failed:%s", err)
	}
}

// define your schema
type YourSchema struct {
	mongoose.Document `bson:",inline"`
	FieldA            string     `bson:"field_a,omitempty"`
	FieldB            int        `bson:"field_b,omitempty"`
	Son               *SubSchema `bson:"son,omitempty"`
	*SubSchema        `bson:",inline"` // inline field
}

type SubSchema struct {
	FieldC string `bson:"field_c,omitempty"`
	FieldD string `bson:"field_d,omitempty"`
}

// CollectionName impl mongoose.IDocument interface
func (*YourSchema) CollectionName() string {
	return "your_collection"
}

func Create() {
	// will auto add create time for now , and also you can set your time 
	// and all create method will auto find collection name
	odm.InsertOne(&YourSchema{
		FieldA: "test",
		FieldB: 3,
		SubSchema: &SubSchema{
			FieldC: "111",
			FieldD: "222",
		},
	})

	// if your want to use driver method
	odm.DriverCollection("your collection").InsertOne(bson.M{})
}

func HaveFilterMethod() {
	// update find delete
	// id support string (primitive.ObjectID.hex()) and ObjectID
	var result YourSchema
	odm.FindByID(id, &result)

	// filter support bson and IDocument
	// This is equivalent
	var result2 []YourSchema
	odm.Find(YourSchema{FieldA: "zhangsan"}, &result2)
	odm.Find(bson.M{"field_a": "zhangsan"}, &result2)
	// this is sugar
	odm.Find(mongoose.Eq("field_a", "zhangsan"), &result2)
}


// release client
func Close() {
	odm.Release(context.TODO())
}

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 ConvertSliceFilter added in v0.1.8

func ConvertSliceFilter(value reflect.Value, tag string) bson.M

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 Now added in v0.1.3

func Now() *time.Time

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 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) Client added in v0.2.2

func (m *Mongo) Client() *mongo.Client

func (*Mongo) CountDocuments added in v0.1.6

func (m *Mongo) CountDocuments(filter IDocument, opts ...*options.CountOptions) (count int64, err error)

func (*Mongo) DB added in v0.2.2

func (m *Mongo) DB() *mongo.Database

func (*Mongo) DeleteMany

func (m *Mongo) DeleteMany(filter IDocument, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

DeleteMany 删除多条数据 如果你希望使用bson当作filter,那么建议使用driver的原始方法

func (*Mongo) DeleteOne

func (m *Mongo) DeleteOne(filter IDocument, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

DeleteOne 删除一条数据 如果你希望使用bson当作filter,那么建议使用driver的原始方法

func (*Mongo) DriverCollection added in v0.1.5

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

DriverCollection 提供原始查询方法

func (*Mongo) Find

func (m *Mongo) Find(filter, 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, result interface{}, opts ...*options.FindOneOptions) (err error)

FindByID 通过id查找数据

func (*Mongo) FindOne

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

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

func (*Mongo) FindOneAndDelete added in v0.1.6

func (m *Mongo) FindOneAndDelete(filter IDocument, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult

FindOneAndDelete 如果你希望使用bson当作filter,那么建议使用driver的原始方法

func (*Mongo) FindOneAndReplace added in v0.1.6

func (m *Mongo) FindOneAndReplace(filter interface{}, replacement IDocument,
	opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult

func (*Mongo) FindOneAndUpdate added in v0.1.6

func (m *Mongo) FindOneAndUpdate(filter interface{}, update IDocument,
	opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult

func (*Mongo) FindOneUndeleteByFilter

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

FindOneUndeleteByFilter 查找一条未删除的数据

func (*Mongo) FindUnDeleteByID

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

FindUnDeleteByID 查找一条未删除的数据

func (*Mongo) FindUndeleteByFilter

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

FindUndeleteByFilter 查找未删除的所有数据

func (*Mongo) InsertMany

func (m *Mongo) InsertMany(docs []interface{}, 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