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 ¶
- func RegisterAllViews() error
- func UnregisterAllViews()
- type WrappedClient
- func (wc *WrappedClient) Client() *mongo.Client
- func (wc *WrappedClient) Connect(ctx context.Context) error
- func (wc *WrappedClient) Database(name string, opts ...*options.DatabaseOptions) *WrappedDatabase
- func (wc *WrappedClient) Disconnect(ctx context.Context) error
- func (wc *WrappedClient) ListDatabaseNames(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) ([]string, error)
- func (wc *WrappedClient) ListDatabases(ctx context.Context, filter interface{}, opts ...*options.ListDatabasesOptions) (mongo.ListDatabasesResult, error)
- func (wc *WrappedClient) NewClientEncryption(opts ...*options.ClientEncryptionOptions) (*WrappedClientEncryption, error)
- func (wc *WrappedClient) Ping(ctx context.Context, rp *readpref.ReadPref) error
- func (wc *WrappedClient) StartSession(opts ...*options.SessionOptions) (mongo.Session, error)
- func (wc *WrappedClient) UseSession(ctx context.Context, fn func(mongo.SessionContext) error) error
- func (wc *WrappedClient) UseSessionWithOptions(ctx context.Context, opts *options.SessionOptions, ...) error
- type WrappedClientEncryption
- func (wce *WrappedClientEncryption) Close(ctx context.Context) error
- func (wce *WrappedClientEncryption) CreateDataKey(ctx context.Context, kmsProvider string, opts ...*options.DataKeyOptions) (primitive.Binary, error)
- func (wce *WrappedClientEncryption) Decrypt(ctx context.Context, val primitive.Binary) (bson.RawValue, error)
- func (wce *WrappedClientEncryption) Encrypt(ctx context.Context, val bson.RawValue, opts ...*options.EncryptOptions) (primitive.Binary, error)
- type WrappedCollection
- func (wc *WrappedCollection) Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)
- func (wc *WrappedCollection) BulkWrite(ctx context.Context, models []mongo.WriteModel, ...) (*mongo.BulkWriteResult, error)
- func (wc *WrappedCollection) Clone(opts ...*options.CollectionOptions) (*mongo.Collection, error)
- func (wc *WrappedCollection) Collection() *mongo.Collection
- func (wc *WrappedCollection) Count(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
- func (wc *WrappedCollection) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
- func (wc *WrappedCollection) Database() *mongo.Database
- func (wc *WrappedCollection) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (wc *WrappedCollection) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (wc *WrappedCollection) Distinct(ctx context.Context, fieldName string, filter interface{}, ...) ([]interface{}, error)
- func (wc *WrappedCollection) Drop(ctx context.Context) error
- func (wc *WrappedCollection) EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
- func (wc *WrappedCollection) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
- func (wc *WrappedCollection) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
- func (wc *WrappedCollection) FindOneAndDelete(ctx context.Context, filter interface{}, ...) *mongo.SingleResult
- func (wc *WrappedCollection) FindOneAndReplace(ctx context.Context, filter, replacement interface{}, ...) *mongo.SingleResult
- func (wc *WrappedCollection) FindOneAndUpdate(ctx context.Context, filter, update interface{}, ...) *mongo.SingleResult
- func (wc *WrappedCollection) Indexes() mongo.IndexView
- func (wc *WrappedCollection) InsertMany(ctx context.Context, documents []interface{}, ...) (*mongo.InsertManyResult, error)
- func (wc *WrappedCollection) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
- func (wc *WrappedCollection) Name() string
- func (wc *WrappedCollection) ReplaceOne(ctx context.Context, filter, replacement interface{}, ...) (*mongo.UpdateResult, error)
- func (wc *WrappedCollection) UpdateMany(ctx context.Context, filter, replacement interface{}, ...) (*mongo.UpdateResult, error)
- func (wc *WrappedCollection) UpdateOne(ctx context.Context, filter, replacement interface{}, ...) (*mongo.UpdateResult, error)
- func (wc *WrappedCollection) Watch(ctx context.Context, pipeline interface{}, ...) (*mongo.ChangeStream, error)
- type WrappedDatabase
- func (wd *WrappedDatabase) Client() *WrappedClient
- func (wd *WrappedDatabase) Collection(name string, opts ...*options.CollectionOptions) *WrappedCollection
- func (wd *WrappedDatabase) Database() *mongo.Database
- func (wd *WrappedDatabase) Drop(ctx context.Context) error
- func (wd *WrappedDatabase) ListCollections(ctx context.Context, filter interface{}, ...) (*mongo.Cursor, error)
- func (wd *WrappedDatabase) Name() string
- func (wd *WrappedDatabase) ReadConcern() *readconcern.ReadConcern
- func (wd *WrappedDatabase) ReadPreference() *readpref.ReadPref
- func (wd *WrappedDatabase) RunCommand(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) *mongo.SingleResult
- func (wd *WrappedDatabase) WriteConcern() *writeconcern.WriteConcern
- type WrappedSession
- func (ws *WrappedSession) AbortTransaction(ctx context.Context) error
- func (ws *WrappedSession) AdvanceClusterTime(br bson.Raw) error
- func (ws *WrappedSession) AdvanceOperationTime(pt *primitive.Timestamp) error
- func (ws *WrappedSession) ClusterTime() bson.Raw
- func (ws *WrappedSession) CommitTransaction(ctx context.Context) error
- func (ws *WrappedSession) EndSession(ctx context.Context)
- func (ws *WrappedSession) OperationTime() *primitive.Timestamp
- func (ws *WrappedSession) StartTransaction(topts ...*options.TransactionOptions) error
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) 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) 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)
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 (wc *WrappedCollection) BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
func (*WrappedCollection) Clone ¶
func (wc *WrappedCollection) Clone(opts ...*options.CollectionOptions) (*mongo.Collection, error)
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) 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) 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 ¶
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
Click to show internal directories.
Click to hide internal directories.