mongo

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2022 License: MIT Imports: 20 Imported by: 0

README

MongoDB Wrapper

Usage

      import (
              "github.com/kelchy/go-lib/mongo"
      )
  • Initialization - for simplicity this example shows getting from env. but developer can opt to use config manager
uri := os.Getenv("MONGOURI")
Mongo, e := mongo.New(uri)
  • Insert doc in collection
        list := []interface{}{}
        inserted := Mongo.InsertMany(nil, 0, "cdr", list, false)
  • Find doc in collection
        cdrs, e := Mongo.Find(nil, 0, "cdr", mongo.M{}, mongo.M{"sort": "start", "order": int64(-1), "skip": int64(0), "limit": int64(100)})
        if e != nil {
                res.Error = e.Error()
                router.JSON(w, r, res)
        }
  • Insert file in gridFS
        filesize, e := Mongo.FSset("/tmp/cdr.csv")
        if e != nil {
                log.Error(e)
        }
  • Find file in gridFS
        files, e := Mongo.FSlist(mongo.M{"metadata.type": listType}, "uploadDate", -1, 0, 100)
        if e != nil {
                log.Error(e)
                router.JSON(w, r, res)
                return
        }
  • Perform transaction for arbitrary number of operations and different types of operations
    actions := []map[string]interface{}{
        {
            "operation": "insertOne", // the type of operation. currently only updateOne, insertOne, insertMany, updateMany are supported
            "collection": "test", // collection name
            "doc": map[string]interface{}{
                "_id": 12345,
                "product": "PRD2",
                "price": 5,
            },
        },
        {
            "operation": "updateOne",
            "collection": "test",
			"filter": map[string]interface{}{
				"product": "PRD6",
			},
			"update" : map[string]interface{}{
				"$set": map[string]interface{}{
					"price": 100,
				},
			},
        },
    }
   response, err := mongoClient.Transaction(actions)

Documentation

Index

Constants

This section is empty.

Variables

A - re-export primitive.A

DateTime - re-export primitive.DateTime

M - re-export primitive.M

View Source
var Operations = []string{
	"updateOne",
	"insertOne",
	"insertMany",
	"updateMany",
}

Operations - list of allowed operations for transactions

Functions

func IntDateTime

func IntDateTime(i interface{}) int64

IntDateTime - the purpose is to convert a primitive.DateTime to int64

func MapInterface

func MapInterface(i interface{}) map[string]interface{}

MapInterface - the purpose is to convert a primitive.M to map[string]interface{}

func SetContext

func SetContext(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

SetContext - helper to set deadline to context

func SliceInterface

func SliceInterface(i interface{}) []interface{}

SliceInterface - the purpose is to convert a primitive.A to []interface{}

func Unmarshal

func Unmarshal(s string) bson.M

Unmarshal - bson unmarshal

Types

type Client

type Client struct {
	URI        string
	Db         *mongo.Database
	Connection *mongo.Client
	// contains filtered or unexported fields
}

Client - instance initiated by constructor

func New

func New(uri string) (Client, error)

New - constructor to initiate client instance

func (Client) Aggregate

func (client Client) Aggregate(ctx context.Context, colname string, pipeline []interface{}) ([]bson.M, error)

Aggregate - function to aggregate docs in collection, ctx can be nil

func (Client) DeleteMany

func (client Client) DeleteMany(ctx context.Context, colname string, filter interface{}) (int64, error)

DeleteMany - function to delete many docs in the collection, ctx can be nil

func (Client) DeleteOne

func (client Client) DeleteOne(ctx context.Context, colname string, filter interface{}) (int64, error)

DeleteOne - function to delete a single doc in the collection, ctx can be nil

func (Client) FSget

func (client Client) FSget(filename string) (io.ReadSeeker, error)

FSget - function to fetch the actual file content

func (Client) FSlist

func (client Client) FSlist(transactionCtx context.Context, filter bson.M, sort string, order int64, skip int64,
	limit int64) ([]bson.M, error)

FSlist - function to fetch the list of files together with its metadata

func (Client) FSset

func (client Client) FSset(filename string) (int, error)

FSset - function to upload the actual file content

func (Client) Find

func (client Client) Find(ctx context.Context, colname string, filter map[string]interface{},
	opt map[string]interface{}) ([]bson.M, error)

Find - function to find doc in collection, ctx can be nil

func (Client) FindOne

func (client Client) FindOne(ctx context.Context, colname string,
	filter map[string]interface{}, opt map[string]interface{}) (*mongo.SingleResult, error)

FindOne - function to find first encountered doc in collection, ctx can be nil

func (Client) InsertMany

func (client Client) InsertMany(ctx context.Context, colname string, docs []interface{}, ordered bool) (
	int, []interface{}, error)

InsertMany - function to insert many docs into collection, ctx can be nil

func (Client) InsertOne

func (client Client) InsertOne(ctx context.Context, colname string, doc interface{}) (interface{}, error)

InsertOne - function to insert a single doc into collection, ctx can be nil

func (Client) ReplaceOne

func (client Client) ReplaceOne(ctx context.Context, colname string,
	filter interface{}, replace interface{}, opts *options.ReplaceOptions) (int64, error)

ReplaceOne - function to replace a single doc in the collection, ctx can be nil

func (Client) Transaction

func (client Client) Transaction(actions []map[string]interface{}, timeout int) (interface{}, error)

Transaction - creates a transaction

func (Client) UpdateMany

func (client Client) UpdateMany(ctx context.Context, colname string, filter interface{},
	update interface{}) (int64, error)

UpdateMany - function to update many docs in the collection, ctx can be nil

func (Client) UpdateOne

func (client Client) UpdateOne(ctx context.Context, colname string,
	filter interface{}, update interface{}, opts *options.UpdateOptions) (int64, error)

UpdateOne - function to update a single doc in the collection, ctx can be nil

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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