mongodb

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRegistry = build()
View Source
var File_pb_ObjectID_proto protoreflect.FileDescriptor
View Source
var File_pb_TimeStamp_proto protoreflect.FileDescriptor

Functions

func Connect

func Connect(mongoUrl string) (*mongo.Client, error)

Types

type Collection

type Collection struct {
	Name string
	// contains filtered or unexported fields
}

func (*Collection) Coll

func (c *Collection) Coll() *mongo.Collection

func (*Collection) CountDocuments

func (c *Collection) CountDocuments(filter any) (int64, error)

统计满足条件的文档数,会遍历整个表 判断条件是否存在请使用 Exists

func (*Collection) CreateIndex

func (coll *Collection) CreateIndex(list []*Index) ([]string, error)

创建索引,不支持联合索引

func (*Collection) CreateIndexByKeys

func (coll *Collection) CreateIndexByKeys(keys ...string)

偷懒写法

func (*Collection) DeleteId

func (c *Collection) DeleteId(id any) error

func (*Collection) DeleteOne

func (c *Collection) DeleteOne(filter any) error

func (*Collection) Exists

func (c *Collection) Exists(filter bson.M) (bool, error)

判断某条件是否存在于表中

func (*Collection) FindAll

func (c *Collection) FindAll(filter bson.M, doc any) error

func (*Collection) FindAllOpt

func (c *Collection) FindAllOpt(filter bson.M, doc any, opt *options.FindOptions) error

func (*Collection) FindId

func (c *Collection) FindId(id, doc any) error

func (*Collection) FindOne

func (c *Collection) FindOne(filter, doc any) error

func (*Collection) FindPage

func (c *Collection) FindPage(args FindPageOpt, doc any) (count int64, err error)

func (*Collection) FindPageFakeCount

func (c *Collection) FindPageFakeCount(args FindPageOpt, doc any) (count int64, err error)

返回假的count,避免用时过长

func (*Collection) InsertOne

func (c *Collection) InsertOne(doc any) error

func (*Collection) Update

func (c *Collection) Update(filter any, update any) error

func (*Collection) UpdateId

func (c *Collection) UpdateId(id any, update any) error

func (*Collection) UpdateOne

func (c *Collection) UpdateOne(id any, update any) error

func (*Collection) Upsert

func (c *Collection) Upsert(filter any, update any) error

func (*Collection) UpsertId

func (c *Collection) UpsertId(id any, update any) error

func (*Collection) UpsertOne

func (c *Collection) UpsertOne(filter any, update any) error

type DB

type DB struct {
	Name   string
	Client *mongo.Client
}

func NewDB

func NewDB(dbName string) *DB

func NewDBWithClient

func NewDBWithClient(dbName string, client *mongo.Client) *DB

func (*DB) Collection

func (db *DB) Collection(collName string) *Collection

func (*DB) Connect

func (db *DB) Connect(dburl string) error

type DocOf

type DocOf[K ut2.Comparable, V any] struct {
	Coll *Collection
	Ctor func(*V) // 在自动创建的时候调用,初始化数据
	Map  ut2.IMap[K, *V]
	// contains filtered or unexported fields
}

管理一张Mongo表。 并支持内存中缓存。自动以 _id 为 Key

func NewDocOf

func NewDocOf[K ut2.Comparable, V any](coll *Collection) *DocOf[K, V]

有 内存缓存

func NewDocOfWith

func NewDocOfWith[K ut2.Comparable, V any](coll *Collection, mp ut2.IMap[K, *V]) *DocOf[K, V]

func NewDocOfWithoutCache

func NewDocOfWithoutCache[K ut2.Comparable, V any](coll *Collection) *DocOf[K, V]

无 内存缓存

func (*DocOf[K, V]) Call

func (d *DocOf[K, V]) Call(key K, fn func(data *V)) error

更新数据

func (*DocOf[K, V]) GetData

func (d *DocOf[K, V]) GetData(pid K) V

获取数据拷贝

func (*DocOf[K, V]) Release

func (d *DocOf[K, V]) Release()

释放内存缓存

type FindPageOpt

type FindPageOpt struct {
	Page     int64 // 从1开始的页码 default = 1
	PageSize int64 // 每页个数 default = 30
	Sort     any   // 排序
	Query    any   // 查找条件
}

type Index

type Index struct {
	Key        string
	Background bool
	SetUnique  bool
	Sparse     bool
	Order      IndexOrder
}

type IndexOrder

type IndexOrder int32
const (
	IndexAscending  IndexOrder = 0 // 升序 = 1 默认
	IndexDescending            = 1 // 降序 = -1
)

type ObjectID

type ObjectID struct {
	Data []byte `protobuf:"bytes,1,opt,name=Data,proto3" `
	// contains filtered or unexported fields
}

func NewObjectID

func NewObjectID() *ObjectID

func ObjectIDFromHex

func ObjectIDFromHex(s string) (*ObjectID, error)

func (*ObjectID) Descriptor deprecated

func (*ObjectID) Descriptor() ([]byte, []int)

Deprecated: Use ObjectID.ProtoReflect.Descriptor instead.

func (*ObjectID) Hex

func (id *ObjectID) Hex() string

func (*ObjectID) MarshalJSON

func (id *ObjectID) MarshalJSON() ([]byte, error)

func (*ObjectID) ProtoMessage

func (*ObjectID) ProtoMessage()

func (*ObjectID) ProtoReflect

func (x *ObjectID) ProtoReflect() protoreflect.Message

func (*ObjectID) Reset

func (x *ObjectID) Reset()

func (*ObjectID) String

func (x *ObjectID) String() string

func (*ObjectID) UnmarshalJSON

func (id *ObjectID) UnmarshalJSON(b []byte) error

type ObjectIDCodec

type ObjectIDCodec struct{}

func (*ObjectIDCodec) DecodeValue

DecodeValue is the ValueDecoderFunc for time.Time.

func (*ObjectIDCodec) EncodeValue

EncodeValue is the ValueEncoderFunc for time.TIme.

type TimeStamp

type TimeStamp struct {
	Seconds int64 `protobuf:"varint,1,opt,name=Seconds,proto3" `
	Nanos   int32 `protobuf:"varint,2,opt,name=Nanos,proto3" `
	// contains filtered or unexported fields
}

func NewTimeStamp

func NewTimeStamp(t time.Time) *TimeStamp

New constructs a new Timestamp from the provided time.Time.

func (*TimeStamp) AsTime

func (x *TimeStamp) AsTime() time.Time

AsTime converts x to a time.Time.

func (*TimeStamp) Descriptor deprecated

func (*TimeStamp) Descriptor() ([]byte, []int)

Deprecated: Use TimeStamp.ProtoReflect.Descriptor instead.

func (*TimeStamp) GetNanos

func (x *TimeStamp) GetNanos() int32

func (*TimeStamp) GetSeconds

func (x *TimeStamp) GetSeconds() int64

func (*TimeStamp) MarshalJSON

func (x *TimeStamp) MarshalJSON() ([]byte, error)

func (*TimeStamp) ProtoMessage

func (*TimeStamp) ProtoMessage()

func (*TimeStamp) ProtoReflect

func (x *TimeStamp) ProtoReflect() protoreflect.Message

func (*TimeStamp) Reset

func (x *TimeStamp) Reset()

func (*TimeStamp) SetTime

func (x *TimeStamp) SetTime(t time.Time)

func (*TimeStamp) String

func (x *TimeStamp) String() string

func (*TimeStamp) UnmarshalJSON

func (x *TimeStamp) UnmarshalJSON(b []byte) error

type TimeStampCodec

type TimeStampCodec struct{}

func (*TimeStampCodec) DecodeValue

DecodeValue is the ValueDecoderFunc for time.Time.

func (*TimeStampCodec) EncodeValue

EncodeValue is the ValueEncoderFunc for time.TIme.

Jump to

Keyboard shortcuts

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