mongowrapper

package module
v0.0.4-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2020 License: Apache-2.0 Imports: 14 Imported by: 0

README

gomongowrapper

MongoDB Go wrapper source code

Table of contents

End to end example

With a MongoDB server running at "localhost:27017" and running this example with Go

package main

import (
	"context"
	"log"
	"time"

	"go.mongodb.org/mongo-driver/mongo/options"

	"go.mongodb.org/mongo-driver/bson"

	mongowrapper "github.com/opencensus-integrations/gomongowrapper"

	"contrib.go.opencensus.io/exporter/stackdriver"
	"go.opencensus.io/stats/view"
	"go.opencensus.io/trace"
)

func main() {
	// Enabling the OpenCensus exporter.
	// Just using Stackdriver since it has both Tracing and Metrics
	// and is easy to whip up. Add your desired one here.
	sde, err := stackdriver.NewExporter(stackdriver.Options{
		ProjectID:    "census-demos",
		MetricPrefix: "mongosample",
	})
	if err != nil {
		log.Fatalf("Failed to create Stackdriver exporter: %v", err)
	}
	view.RegisterExporter(sde)
	trace.RegisterExporter(sde)
	if err := mongowrapper.RegisterAllViews(); err != nil {
		log.Fatalf("Failed to register all views: %v\n", err)
	}

	defer func() {
		<-time.After(2 * time.Minute)
	}()

	// Start a span like your application would start one.
	ctx, span := trace.StartSpan(context.Background(), "Fetch", trace.WithSampler(trace.AlwaysSample()))
	defer span.End()

        // Now for the mongo connections, using the context
        // with the span in it for continuity.
	client, err := mongowrapper.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
	if err != nil {
		log.Fatalf("Failed to create the new client: %v", err)
	}
	if err := client.Connect(ctx); err != nil {
		log.Fatalf("Failed to open client connection: %v", err)
	}
	defer client.Disconnect(ctx)
	coll := client.Database("the_db").Collection("music")

	q := bson.M{"name": "Examples"}
	cur, err := coll.Find(ctx, q)
	if err != nil {
		log.Fatalf("Find error: %v", err)
	}

	for cur.Next(ctx) {
		elem := make(map[string]int)
		if err := cur.Decode(elem); err != nil {
			log.Printf("Decode error: %v", err)
			continue
		}
		log.Printf("Got result: %v\n", elem)
	}
	log.Print("Done iterating")

	_, err = coll.DeleteMany(ctx, q)
	if err != nil {
		log.Fatalf("Failed to delete: %v", err)
	}
}

Traces

Metrics

Documentation

Overview

Example
package main

import (
	"context"
	"log"
	"time"

	"go.mongodb.org/mongo-driver/mongo/options"

	"go.mongodb.org/mongo-driver/bson"

	mongowrapper "github.com/opencensus-integrations/gomongowrapper"

	"contrib.go.opencensus.io/exporter/stackdriver"
	"go.opencensus.io/stats/view"
	"go.opencensus.io/trace"
)

func main() {
	// Enabling the OpenCensus exporter.
	// Just using Stackdriver since it has both Tracing and Metrics
	// and is easy to whip up. Add your desired one here.
	sde, err := stackdriver.NewExporter(stackdriver.Options{
		ProjectID:    "census-demos",
		MetricPrefix: "mongosample",
	})
	if err != nil {
		log.Fatalf("Failed to create Stackdriver exporter: %v", err)
	}
	view.RegisterExporter(sde)
	trace.RegisterExporter(sde)
	if err := mongowrapper.RegisterAllViews(); err != nil {
		log.Fatalf("Failed to register all views: %v\n", err)
	}

	defer func() {
		<-time.After(2 * time.Minute)
	}()

	// Start a span like your application would start one.
	ctx, span := trace.StartSpan(context.Background(), "Fetch", trace.WithSampler(trace.AlwaysSample()))
	defer span.End()

	// Now for the mongo connections, using the context
	// with the span in it for continuity.
	client, err := mongowrapper.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
	if err != nil {
		log.Fatalf("Failed to create the new client: %v", err)
	}
	if err := client.Connect(ctx); err != nil {
		log.Fatalf("Failed to open client connection: %v", err)
	}
	defer client.Disconnect(ctx)
	coll := client.Database("the_db").Collection("music")

	q := bson.M{"name": "Examples"}
	cur, err := coll.Find(ctx, q)
	if err != nil {
		log.Fatalf("Find error: %v", err)
	}

	for cur.Next(ctx) {
		elem := make(map[string]int)
		if err := cur.Decode(elem); err != nil {
			log.Printf("Decode error: %v", err)
			continue
		}
		log.Printf("Got result: %v\n", elem)
	}
	log.Print("Done iterating")

	_, err = coll.DeleteMany(ctx, q)
	if err != nil {
		log.Fatalf("Failed to delete: %v", err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterAllViews

func RegisterAllViews() error

func UnregisterAllViews

func UnregisterAllViews()

Types

type WrappedClient

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

func Connect

func Connect(ctx context.Context, opts ...*options.ClientOptions) (*WrappedClient, error)

func NewClient

func NewClient(opts ...*options.ClientOptions) (*WrappedClient, error)

func (*WrappedClient) Client

func (wc *WrappedClient) Client() *mongo.Client

func (*WrappedClient) Connect

func (wc *WrappedClient) Connect(ctx context.Context) error

func (*WrappedClient) Database

func (wc *WrappedClient) Database(name string, opts ...*options.DatabaseOptions) *WrappedDatabase

func (*WrappedClient) Disconnect

func (wc *WrappedClient) Disconnect(ctx context.Context) error

func (*WrappedClient) ListDatabaseNames

func (wc *WrappedClient) ListDatabaseNames(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) ([]string, error)

func (*WrappedClient) ListDatabases

func (wc *WrappedClient) ListDatabases(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) (mongo.ListDatabasesResult, error)

func (*WrappedClient) NewClientEncryption

func (wc *WrappedClient) NewClientEncryption(opts ...*options.ClientEncryptionOptions) (*WrappedClientEncryption, error)

func (*WrappedClient) Ping

func (wc *WrappedClient) Ping(ctx context.Context, rp *readpref.ReadPref) error

func (*WrappedClient) StartSession

func (wc *WrappedClient) StartSession(opts ...*options.SessionOptions) (mongo.Session, error)

func (*WrappedClient) UseSession

func (wc *WrappedClient) UseSession(ctx context.Context, fn func(mongo.SessionContext) error) error

func (*WrappedClient) UseSessionWithOptions

func (wc *WrappedClient) UseSessionWithOptions(ctx context.Context, opts *options.SessionOptions, fn func(mongo.SessionContext) error) error

type WrappedClientEncryption

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

func (*WrappedClientEncryption) Close

func (wce *WrappedClientEncryption) Close(ctx context.Context) error

func (*WrappedClientEncryption) CreateDataKey

func (wce *WrappedClientEncryption) CreateDataKey(ctx context.Context, kmsProvider string, opts ...*options.DataKeyOptions) (primitive.Binary, error)

func (*WrappedClientEncryption) Decrypt

func (*WrappedClientEncryption) Encrypt

type WrappedCollection

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

func (*WrappedCollection) Aggregate

func (wc *WrappedCollection) Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)

func (*WrappedCollection) BulkWrite

func (*WrappedCollection) Clone

func (*WrappedCollection) Collection

func (wc *WrappedCollection) Collection() *mongo.Collection

func (*WrappedCollection) Count

func (wc *WrappedCollection) Count(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)

func (*WrappedCollection) CountDocuments

func (wc *WrappedCollection) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)

func (*WrappedCollection) Database

func (wc *WrappedCollection) Database() *mongo.Database

func (*WrappedCollection) DeleteMany

func (wc *WrappedCollection) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

func (*WrappedCollection) DeleteOne

func (wc *WrappedCollection) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

func (*WrappedCollection) Distinct

func (wc *WrappedCollection) Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)

func (*WrappedCollection) Drop

func (wc *WrappedCollection) Drop(ctx context.Context) error

func (*WrappedCollection) EstimatedDocumentCount

func (wc *WrappedCollection) EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)

func (*WrappedCollection) Find

func (wc *WrappedCollection) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)

func (*WrappedCollection) FindOne

func (wc *WrappedCollection) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult

func (*WrappedCollection) FindOneAndDelete

func (wc *WrappedCollection) FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult

func (*WrappedCollection) FindOneAndReplace

func (wc *WrappedCollection) FindOneAndReplace(ctx context.Context, filter, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult

func (*WrappedCollection) FindOneAndUpdate

func (wc *WrappedCollection) FindOneAndUpdate(ctx context.Context, filter, update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult

func (*WrappedCollection) Indexes

func (wc *WrappedCollection) Indexes() mongo.IndexView

func (*WrappedCollection) InsertMany

func (wc *WrappedCollection) InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)

func (*WrappedCollection) InsertOne

func (wc *WrappedCollection) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)

func (*WrappedCollection) Name

func (wc *WrappedCollection) Name() string

func (*WrappedCollection) ReplaceOne

func (wc *WrappedCollection) ReplaceOne(ctx context.Context, filter, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)

func (*WrappedCollection) UpdateMany

func (wc *WrappedCollection) UpdateMany(ctx context.Context, filter, replacement interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func (*WrappedCollection) UpdateOne

func (wc *WrappedCollection) UpdateOne(ctx context.Context, filter, replacement interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func (*WrappedCollection) Watch

func (wc *WrappedCollection) Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)

type WrappedDatabase

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

func (*WrappedDatabase) Client

func (wd *WrappedDatabase) Client() *WrappedClient

func (*WrappedDatabase) Collection

func (wd *WrappedDatabase) Collection(name string, opts ...*options.CollectionOptions) *WrappedCollection

func (*WrappedDatabase) Database

func (wd *WrappedDatabase) Database() *mongo.Database

func (*WrappedDatabase) Drop

func (wd *WrappedDatabase) Drop(ctx context.Context) error

func (*WrappedDatabase) ListCollections

func (wd *WrappedDatabase) ListCollections(ctx context.Context, filter interface{}, opts ...*options.ListCollectionsOptions) (*mongo.Cursor, error)

func (*WrappedDatabase) Name

func (wd *WrappedDatabase) Name() string

func (*WrappedDatabase) ReadConcern

func (wd *WrappedDatabase) ReadConcern() *readconcern.ReadConcern

func (*WrappedDatabase) ReadPreference

func (wd *WrappedDatabase) ReadPreference() *readpref.ReadPref

func (*WrappedDatabase) RunCommand

func (wd *WrappedDatabase) RunCommand(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) *mongo.SingleResult

func (*WrappedDatabase) WriteConcern

func (wd *WrappedDatabase) WriteConcern() *writeconcern.WriteConcern

type WrappedSession

type WrappedSession struct {
	mongo.Session
}

func (*WrappedSession) AbortTransaction

func (ws *WrappedSession) AbortTransaction(ctx context.Context) error

func (*WrappedSession) AdvanceClusterTime

func (ws *WrappedSession) AdvanceClusterTime(br bson.Raw) error

func (*WrappedSession) AdvanceOperationTime

func (ws *WrappedSession) AdvanceOperationTime(pt *primitive.Timestamp) error

func (*WrappedSession) ClusterTime

func (ws *WrappedSession) ClusterTime() bson.Raw

func (*WrappedSession) CommitTransaction

func (ws *WrappedSession) CommitTransaction(ctx context.Context) error

func (*WrappedSession) EndSession

func (ws *WrappedSession) EndSession(ctx context.Context)

func (*WrappedSession) OperationTime

func (ws *WrappedSession) OperationTime() *primitive.Timestamp

func (*WrappedSession) StartTransaction

func (ws *WrappedSession) StartTransaction(topts ...*options.TransactionOptions) error

Jump to

Keyboard shortcuts

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