mongobj

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package mongobj implements a simple interface to access mongodb. 將自定義的資料結構藉由Primary Key存入與取出mongodb,而不使用內建的ObjectID(_id)。

TODO: 將此package移動到獨立的repo

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mongolection

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

Mongolection means mongo object collection

func (*Mongolection) Get

func (m *Mongolection) Get(pk bson.M, resultType reflect.Type) (interface{}, error)

Get will find values for pk. If there are no values for pk, nil without error will be returned. 藉由primary key取回自定義資料結構,取回為自定義資料結構的指標。

Ex.
	_ = m.Get(bson.M{"PK": "1234567"}, reflect.TypeOf(MyStructure{})).(*MyStructure)

func (*Mongolection) InitializeUniqueKey

func (m *Mongolection) InitializeUniqueKey(keys ...string) error

InitializeUniqueKey 用來初始化Primary Key,多次設定相同的schema不會造成問題,這個package目前基於pk來存取物件。

TODO: 怎麼有效讓程式碼只設定一次。因為檢查與設定大概都會消耗存取次數,基本上應該沒什麼太大差別?

func (*Mongolection) Set

func (m *Mongolection) Set(pk bson.M, values interface{}) error

Set will upsert values for pk, values should include pk.

Ex.
	type MyStructure struct {
		ID primitive.ObjectID `json:"-" bson:"_id,omitempty"`
		MyPk string `json:"myPk" binding:"required" bson:"myPk"`
		MyName string `json:"myName" binding:"required" bson:"myName"`
	}

	values := MyStructure{MyPk:"1234567", MyName:"Honey"}
	_ = m.Set(bson.M{"myPk": "1234567", values}, values)

func (Mongolection) SetBulk

func (m Mongolection) SetBulk(pks []bson.M, bulkValues []interface{}) error

type Mongorg

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

Mongorg means mongo object organizer

func New

func New(dbUri string) (*Mongorg, error)

func (*Mongorg) Collection

func (m *Mongorg) Collection(dbName string, collName string) *Mongolection

func (*Mongorg) Destroy

func (m *Mongorg) Destroy() error

Jump to

Keyboard shortcuts

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