mongodb

package module
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 10 Imported by: 26

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithPassword added in v0.27.0

func WithPassword(password string) testcontainers.CustomizeRequestOption

WithPassword sets the initial password of the user to be created when the container starts It is used in conjunction with WithUsername to set a username and its password. It will set the superuser password for MongoDB.

func WithReplicaSet added in v0.31.0

func WithReplicaSet(replSetName string) testcontainers.CustomizeRequestOption

WithReplicaSet sets the replica set name for Single node MongoDB replica set.

func WithUsername added in v0.27.0

func WithUsername(username string) testcontainers.CustomizeRequestOption

WithUsername sets the initial username to be created when the container starts It is used in conjunction with WithPassword to set a username and its password. It will create the specified user with superuser power.

Types

type MongoDBContainer

type MongoDBContainer struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

MongoDBContainer represents the MongoDB container type used in the module

func Run added in v0.32.0

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*MongoDBContainer, error)

Run creates an instance of the MongoDB container type

Example
// runMongoDBContainer {
ctx := context.Background()

mongodbContainer, err := mongodb.Run(ctx, "mongo:6")
defer func() {
	if err := testcontainers.TerminateContainer(mongodbContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

state, err := mongodbContainer.State(ctx)
if err != nil {
	log.Printf("failed to get container state: %s", err)
	return
}

fmt.Println(state.Running)
Output:

true
Example (Connect)
// connectToMongo {
ctx := context.Background()

mongodbContainer, err := mongodb.Run(ctx, "mongo:6")
defer func() {
	if err := testcontainers.TerminateContainer(mongodbContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

endpoint, err := mongodbContainer.ConnectionString(ctx)
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

mongoClient, err := mongo.Connect(options.Client().ApplyURI(endpoint))
if err != nil {
	log.Printf("failed to connect to MongoDB: %s", err)
	return
}
// }

err = mongoClient.Ping(ctx, nil)
if err != nil {
	log.Printf("failed to ping MongoDB: %s", err)
	return
}

fmt.Println(mongoClient.Database("test").Name())
Output:

test
Example (WithCredentials)
ctx := context.Background()

ctr, err := mongodb.Run(ctx,
	"mongo:6",
	mongodb.WithUsername("root"),
	mongodb.WithPassword("password"),
)
defer func() {
	if err := testcontainers.TerminateContainer(ctr); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}

connStr, err := ctr.ConnectionString(ctx)
if err != nil {
	log.Printf("failed to get connection string: %s", err)
	return
}

mongoClient, err := mongo.Connect(options.Client().ApplyURI(connStr))
if err != nil {
	log.Printf("failed to connect to MongoDB: %s", err)
	return
}

err = mongoClient.Ping(ctx, nil)
if err != nil {
	log.Printf("failed to ping MongoDB: %s", err)
	return
}
fmt.Println(strings.Split(connStr, "@")[0])
Output:

mongodb://root:password

func RunContainer deprecated

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*MongoDBContainer, error)

Deprecated: use Run instead RunContainer creates an instance of the MongoDB container type

func (*MongoDBContainer) ConnectionString

func (c *MongoDBContainer) ConnectionString(ctx context.Context) (string, error)

ConnectionString returns the connection string for the MongoDB container. If you provide a username and a password, the connection string will also include them.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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