mongodb

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: MIT Imports: 13 Imported by: 0

README

go-mongodb

Go utilities for MongoDB

Install
go get github.com/uscott/go-mongodb
Usage

Refer to examples directory


type X struct {
	Timestamp time.Time `"timestamp"`
	A         int       `"a"`
	B         float64   `"b`
}

func main() {

	ctx := context.TODO()
	logger := clog.StandardLogger()
	uri := os.Getenv("MONGODB_LOCAL")

	_, db, err := mongodb.GetDB(ctx, uri, "test")
	if err != nil {
		logger.Fatal(err)
	}

	collname := "wat"

	collection, err := mongodb.GetOrCreateCollection(ctx, db, collname)
	if err != nil {
		logger.Fatal(err)
	}

	doc := &X{
		Timestamp: time.Now().UTC(),
		A:         3,
		B:         4.7,
	}

	var result *mongo.InsertOneResult

	if result, err = collection.InsertOne(ctx, doc); err != nil {
		logger.Fatal(err)
	}

	if result != nil {
		fmt.Printf("\nResult= %+v\n", *result)
	}

	ok, err := mongodb.GetLastDocByTimeStamp(ctx, "timestamp", doc, collection)
	if err != nil {
		logger.Fatal(err)
	}
	if ok {
		fmt.Printf("\nX = %+v\n", doc)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectionFind added in v0.1.3

func CollectionFind(
	ctx context.Context,
	uri, dbname, collection string,
	filter interface{},
	opts ...*options.FindOptions) (cursor *mongo.Cursor, err error)

func CollectionInsert added in v0.1.2

func CollectionInsert(doc interface{}, uri, dbname, collection string) (err error)

func CollectionInsertMany added in v0.1.3

func CollectionInsertMany(docs []interface{}, uri, dbname, collection string) (err error)

func CopyCollection

func CopyCollection(ctx context.Context, src, dst *mongo.Collection) (err error)

func GetCollection added in v0.1.3

func GetCollection(ctx context.Context, uri, dbname, collection string) (*mongo.Collection, error)

func GetDB

func GetDB(ctx context.Context, uri, db string) (*mongo.Client, *mongo.Database, error)

func GetLastDocByTimeStamp added in v0.1.5

func GetLastDocByTimeStamp(
	ctx context.Context,
	stampname string,
	doc interface{},
	collection *mongo.Collection) (ok bool, err error)

func GetOrCreateCollection

func GetOrCreateCollection(
	ctx context.Context, db *mongo.Database, name string) (*mongo.Collection, error)

func GetTimeStampRangeFilter added in v0.1.12

func GetTimeStampRangeFilter(tsname string, start, end *time.Time) bson.M

func PrintCollection

func PrintCollection(ctx context.Context, collection *mongo.Collection) (err error)

func RemoveDupesByTimeStamp

func RemoveDupesByTimeStamp(
	ctx context.Context, stampname string, collection *mongo.Collection) (err error)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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