Documentation
¶
Overview ¶
Example ¶
package main import ( "context" "fmt" "leaf/db/mongodb" ) func main() { c, err := mongodb.Connect("mongodb://localhost:27017", 10) if err != nil { fmt.Println(err) return } defer c.Close() // client s, err := c.Ref() if err != nil { fmt.Println(err) return } defer c.UnRef(s) // Remove test document if it exists collRef := s.Database("test").Collection("counters") _, err = collRef.DeleteOne(context.TODO(), map[string]interface{}{"_id": "test"}) // No need to check for NotFound error as DeleteOne doesn't return it // auto increment err = c.EnsureCounter("test", "counters", "test") if err != nil { fmt.Println(err) return } for i := 0; i < 3; i++ { id, err := c.NextSeq("test", "counters", "test") if err != nil { fmt.Println(err) return } fmt.Println(id) } // index c.EnsureUniqueIndex("test", "counters", []string{"key1"}) }
Output: 1 2 3
Index ¶
- type Client
- type ClientHeap
- type ConnectionContext
- func (c *ConnectionContext) Close()
- func (c *ConnectionContext) EnsureCounter(db, collection, id string) error
- func (c *ConnectionContext) EnsureIndex(db, collection string, key []string) error
- func (c *ConnectionContext) EnsureUniqueIndex(db, collection string, key []string) error
- func (c *ConnectionContext) NextSeq(db, collection, id string) (int, error)
- func (c *ConnectionContext) Ref() (*Client, error)
- func (c *ConnectionContext) UnRef(s *Client)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientHeap ¶
type ClientHeap []*Client
Client heap
func (ClientHeap) Len ¶
func (h ClientHeap) Len() int
func (ClientHeap) Less ¶
func (h ClientHeap) Less(i, j int) bool
func (*ClientHeap) Pop ¶
func (h *ClientHeap) Pop() interface{}
func (*ClientHeap) Push ¶
func (h *ClientHeap) Push(s interface{})
func (ClientHeap) Swap ¶
func (h ClientHeap) Swap(i, j int)
type ConnectionContext ¶
func ConnectWithTimeout ¶
func ConnectWithTimeout(url string, clientNum int, connectTimeout, timeout time.Duration) (*ConnectionContext, error)
goroutine safe
func (*ConnectionContext) EnsureCounter ¶
func (c *ConnectionContext) EnsureCounter(db, collection, id string) error
goroutine safe
func (*ConnectionContext) EnsureIndex ¶
func (c *ConnectionContext) EnsureIndex(db, collection string, key []string) error
goroutine safe
func (*ConnectionContext) EnsureUniqueIndex ¶
func (c *ConnectionContext) EnsureUniqueIndex(db, collection string, key []string) error
goroutine safe
Click to show internal directories.
Click to hide internal directories.