mongodb

package
v0.0.0-...-b9eecfe Latest Latest
Warning

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

Go to latest
Published: May 7, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	*mongo.Client
	// contains filtered or unexported fields
}

Client holds a MongoDB client

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

type ConnectionContext struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func Connect

func Connect(url string, clientNum int) (*ConnectionContext, error)

goroutine safe

func ConnectWithTimeout

func ConnectWithTimeout(url string, clientNum int, connectTimeout, timeout time.Duration) (*ConnectionContext, error)

goroutine safe

func (*ConnectionContext) Close

func (c *ConnectionContext) Close()

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

func (*ConnectionContext) NextSeq

func (c *ConnectionContext) NextSeq(db, collection, id string) (int, error)

goroutine safe

func (*ConnectionContext) Ref

func (c *ConnectionContext) Ref() (*Client, error)

goroutine safe

func (*ConnectionContext) UnRef

func (c *ConnectionContext) UnRef(s *Client)

goroutine safe

Jump to

Keyboard shortcuts

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