gophermongo

package module
v0.0.0-...-b54bd16 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: MIT Imports: 6 Imported by: 2

Documentation

Index

Constants

View Source
const Author = "github.com/lordofthemind"

Author of the gophermongo package

View Source
const Description = "" /* 152-byte string literal not displayed */

Description of the gophermongo package

View Source
const Version = "1.0.1"

Version of the gophermongo package

Variables

This section is empty.

Functions

func ConnectToMongoDB

func ConnectToMongoDB(ctx context.Context, dsn string, timeout time.Duration, maxRetries int) (*mongo.Client, error)

ConnectToMongoDB establishes a connection to MongoDB with retries and a context timeout.

This function attempts to connect to MongoDB using the provided connection string (DSN), retrying the connection up to 'maxRetries' times with a delay of 5 seconds between retries. It also applies a timeout to the entire connection attempt using the context.

Params:

ctx - The context for connection management (with timeout support).
dsn - The MongoDB connection string (Data Source Name).
timeout - The timeout duration for the connection attempt.
maxRetries - The maximum number of retries before giving up.

Returns:

*mongo.Client - The connected MongoDB client instance on success.
error - An error message if the connection fails.

Example usage:

ctx := context.Background()
client, err := ConnectToMongoDB(ctx, "mongodb://localhost:27017", 10*time.Second, 3)
if err != nil {
    log.Fatalf("Failed to connect to MongoDB: %v", err)
}
defer client.Disconnect(ctx)

func GetCollection

func GetCollection(db *mongo.Database, collectionName string) *mongo.Collection

GetCollection retrieves the specified MongoDB collection from the database.

This function allows you to get a reference to a specific collection within a MongoDB database. Once you have the collection, you can perform CRUD operations on it.

Params:

db - The MongoDB database instance.
collectionName - The name of the collection to retrieve.

Returns:

*mongo.Collection - The MongoDB collection instance.

Example usage:

database := GetDatabase(client, "myDatabase")
collection := GetCollection(database, "myCollection")

After retrieving the collection, you can perform operations like insert, find, update, or delete.

func GetDatabase

func GetDatabase(client *mongo.Client, dbName string) *mongo.Database

GetDatabase retrieves the specified MongoDB database instance from the client.

This function is a simple utility to get a reference to a MongoDB database using an existing client. It allows interaction with collections within the specified database.

Params:

client - The MongoDB client instance.
dbName - The name of the database to retrieve.

Returns:

*mongo.Database - The MongoDB database instance.

Example usage:

database := GetDatabase(client, "myDatabase")
collection := database.Collection("myCollection")

Once you have the database, you can access any collection within it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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