mongo

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2023 License: MIT Imports: 17 Imported by: 2

README

Mongo Build Status Go Report Card Coverage Status

Provides helpers on top of mongo-go-driver

Version 2, for the official mongo-go-driver

note: version 1 (mgo based) is deprecated

  • Install and update - go get -u github.com/go-pkgz/mongo/v2

  • Connect - Connects with mongo url and return mongo client. Supports extra url params to pass a set of custom values in the url, for example "mongodb://127.0.0.1:27017/test?debug=true&foo=bar. Connect returns mongo.Client as well as the map with all extra key/values. After connect call it also tries to ping the mongo server.

    opts := options.Client().SetAppName("test-app")
    m, params err := Connect(ctx, opts, "mongodb://127.0.0.1:27017/test?debug=true&name=abcd", "debug", "name")
    if err != nil {
        panic("can't make mongo server")
    }
    log.Printf("%+v", params) // prints {"debug":true, "name":"abcd"} 
  • BufferedWriter implements buffered writer to mongo. Write method caching internally till it reached buffer size. Flush methods can be called manually at any time.

    • WithCollection sets collection name to write to
    • WithAutoFlush sets auto flush duration
  • PrepSort - prepares sort object bson.D from strings like "a,-b"

  • PrepIndex - prepares index object driver.IndexModel from strings like "a,-b"

Testing
  • mongo.MakeTestConnection creates mongo.Client and mongo.Collection for url defined in env MONGO_TEST. If not definedmongodb://mongo:27017 used. By default it will use random connection with prefix test_ in test DB.
  • MakeTestConnectionWithColl creates mongo.Client and mongo.Collection with a custom collection name.

Both methods return mongo.Client and mongo.Collection which can be used for testing. It also returns teardown func to clean mongo after the test.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind added in v2.1.0

func Bind(r io.Reader, v interface{}) error

Bind request json body from io.Reader to bson record

func Connect

func Connect(ctx context.Context, opts *options.ClientOptions, u string, extras ...string) (*driver.Client, map[string]interface{}, error)

Connect to mongo url and return client. Supports expanded url params to pass a set of custom values in the url

func MakeTestConnection

func MakeTestConnection(t *testing.T) (mg *driver.Client, coll *driver.Collection, teardown func())

MakeTestConnection connects to MONGO_TEST url or "mongo" host (in no env) and returns new connection. collection name randomized on each call

func MakeTestConnectionWithColl added in v2.1.0

func MakeTestConnectionWithColl(t *testing.T, cname string) (mg *driver.Client, coll *driver.Collection, teardown func())

MakeTestConnectionWithColl connects to MONGO_TEST url or "mongo" host (in no env) and returns new connection. collection name passed in as cname param

func PrepIndex added in v2.1.0

func PrepIndex(keys ...string) driver.IndexModel

PrepIndex prepares index params for mongo driver and returns IndexModel

func PrepSort added in v2.1.0

func PrepSort(sort ...string) bson.D

PrepSort prepares sort params for mongo driver and returns bson.D Input string provided as [+|-]field1,[+|-]field2,[+|-]field3... + means ascending, - means descending. Lack of + or - in the beginning of the field name means ascending sort.

func SecretsMongoUrls added in v2.2.0

func SecretsMongoUrls(urls ...string) (res []string)

SecretsMongoUrls retrieves passwords from mongo urls. this is needed to pass mongo password to the logging masking function

Types

type BufferedWriter

type BufferedWriter interface {
	Write(rec interface{}) error
	Flush() error
	Close() error
}

BufferedWriter defines interface for writes and flush

type BufferedWriterMongo

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

BufferedWriterMongo collects records in local buffer and flushes them as filled. Thread safe by default using both DB and collection from provided connection. Collection can be customized by WithCollection method. Optional flush duration to save on interval

func NewBufferedWriter

func NewBufferedWriter(client *driver.Client, db, collection string, size int) *BufferedWriterMongo

NewBufferedWriter makes batch writer for given size and connection

func (*BufferedWriterMongo) Close

func (bw *BufferedWriterMongo) Close() (err error)

Close flushes all in-fly records and terminates background auto-flusher

func (*BufferedWriterMongo) Flush

func (bw *BufferedWriterMongo) Flush() error

Flush writes everything left in buffer to mongo

func (*BufferedWriterMongo) WithAutoFlush

func (bw *BufferedWriterMongo) WithAutoFlush(duration time.Duration) *BufferedWriterMongo

WithAutoFlush sets auto flush duration

func (*BufferedWriterMongo) WithCollection

func (bw *BufferedWriterMongo) WithCollection(collection string) *BufferedWriterMongo

WithCollection sets custom collection to use with writer

func (*BufferedWriterMongo) Write

func (bw *BufferedWriterMongo) Write(rec interface{}) error

Write to buffer and, as filled, to mongo. If flushDuration defined check for automatic flush

Jump to

Keyboard shortcuts

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