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 ¶
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)
type Mongorg ¶
type Mongorg struct {
// contains filtered or unexported fields
}
Mongorg means mongo object organizer
func (*Mongorg) Collection ¶
func (m *Mongorg) Collection(dbName string, collName string) *Mongolection
Click to show internal directories.
Click to hide internal directories.