Documentation
¶
Overview ¶
Package mongodb provides a MongoDB client wrapper with simplified operations and automatic reconnection.
This package offers a convenient wrapper around the official MongoDB Go driver, providing simplified method signatures for common operations and automatic connection management.
Features:
- Automatic connection and reconnection handling
- CRUD operations with type-safe results
- Aggregation pipeline support
- Bulk write operations
- Index management
- Context-based timeout control
Example:
var client mongodb.Client
client.Initialize("localhost:27017", 10*time.Second)
defer client.Finalize()
client.InsertOne("mydb", "users", bson.M{"name": "Alice", "age": 30})
result, _ := client.FindOne("mydb", "users", bson.M{"name": "Alice"}, User{})
Index ¶
- type Client
- func (c *Client) DeleteMany(databaseName, collectionName string, filter any) error
- func (c *Client) DeleteOne(databaseName, collectionName string, filter any) error
- func (c *Client) Finalize() error
- func (c *Client) Find(databaseName, collectionName string, filter, dataForm any) (any, error)
- func (c *Client) FindOne(databaseName, collectionName string, filter any, dataForm any) (any, error)
- func (c *Client) Initialize(address string, timeout time.Duration) error
- func (c *Client) InsertMany(databaseName, collectionName string, documents []any) error
- func (c *Client) InsertOne(databaseName, collectionName string, document any) error
- func (c *Client) UpdateMany(databaseName, collectionName string, filter, update any) error
- func (c *Client) UpdateOne(databaseName, collectionName string, filter, update any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a struct that provides client related methods.
func (*Client) DeleteMany ¶
DeleteMany deletes all documents matching the filter.
Parameters:
- databaseName: Name of the database
- collectionName: Name of the collection
- filter: BSON filter to match documents (e.g., bson.M{"age": bson.M{"$lt": 25}})
Returns error if client is not initialized, connection fails, or deletion fails.
All matching documents are deleted in a single operation. Use an empty filter bson.M{} to delete all documents in the collection.
Example:
err := client.DeleteMany("mydb", "users", bson.M{"age": bson.M{"$lt": 25}})
// Delete all documents
err = client.DeleteMany("mydb", "users", bson.M{})
func (*Client) DeleteOne ¶
DeleteOne deletes a single document matching the filter.
Parameters:
- databaseName: Name of the database
- collectionName: Name of the collection
- filter: BSON filter to match the document (e.g., bson.M{"_id": 1})
Returns error if client is not initialized, connection fails, or deletion fails.
Only the first matching document is deleted.
Example:
err := client.DeleteOne("mydb", "users", bson.M{"name": "Alice"})
func (*Client) Finalize ¶
Finalize closes the MongoDB connection and releases resources.
Returns error if disconnection fails.
This method should be called when the client is no longer needed, typically using defer after Initialize.
Example:
var client mongodb.Client
client.Initialize("localhost:27017", 10*time.Second)
defer client.Finalize()
func (*Client) Find ¶
Find finds all documents matching the filter and returns them as a slice of the specified type.
Parameters:
- databaseName: Name of the database
- collectionName: Name of the collection
- filter: BSON filter document (e.g., bson.M{"age": bson.M{"$gte": 25}})
- dataForm: Template for the result element type (e.g., User{})
Returns a slice of documents as an interface that must be type-asserted to []dataForm type. Returns error if client is not initialized, connection fails, or query fails.
Use an empty filter bson.M{} to retrieve all documents in the collection. The method uses reflection to create a properly typed slice from the dataForm template.
Example:
results, err := client.Find("mydb", "users", bson.M{"age": bson.M{"$gte": 25}}, User{})
if err != nil {
return err
}
users, ok := results.([]User)
func (*Client) FindOne ¶
func (c *Client) FindOne(databaseName, collectionName string, filter any, dataForm any) (any, error)
FindOne finds a single document matching the filter and returns it as the specified type.
Parameters:
- databaseName: Name of the database
- collectionName: Name of the collection
- filter: BSON filter document (e.g., bson.M{"name": "Alice"})
- dataForm: Template for the result type (e.g., User{})
Returns the found document as an interface that must be type-asserted to the dataForm type. Returns error if client is not initialized, connection fails, or no document is found.
The method uses reflection to create a properly typed result from the dataForm template.
Example:
result, err := client.FindOne("mydb", "users", bson.M{"name": "Alice"}, User{})
if err != nil {
return err
}
user, ok := result.(User)
func (*Client) Initialize ¶
Initialize initializes the MongoDB client with connection settings.
Parameters:
- address: MongoDB server address in format "host:port" (e.g., "localhost:27017")
- timeout: Operation timeout duration for database operations
Returns error if initial connection fails.
The client automatically reconnects on subsequent operations if the connection is lost. Call Finalize() to properly close the connection when done.
Example:
var client mongodb.Client
err := client.Initialize("localhost:27017", 10*time.Second)
defer client.Finalize()
func (*Client) InsertMany ¶
InsertMany inserts multiple documents into the collection in a single operation.
Parameters:
- databaseName: Name of the database
- collectionName: Name of the collection
- documents: Slice of documents to insert ([]any containing structs, bson.M, or bson.D)
Returns error if client is not initialized, connection fails, or insertion fails.
All documents are inserted in a single batch operation for efficiency.
Example:
docs := []any{
User{ID: 1, Name: "Alice", Age: 30},
User{ID: 2, Name: "Bob", Age: 25},
bson.M{"_id": 3, "name": "Charlie", "age": 35},
}
err := client.InsertMany("mydb", "users", docs)
func (*Client) InsertOne ¶
InsertOne inserts a single document into the collection.
Parameters:
- databaseName: Name of the database
- collectionName: Name of the collection
- document: Document to insert (struct, bson.M, or bson.D)
Returns error if client is not initialized, connection fails, or insertion fails.
The document can be a struct with bson tags or a bson.M/bson.D map.
Example:
err := client.InsertOne("mydb", "users", User{ID: 1, Name: "Alice", Age: 30})
// Or with bson.M
err = client.InsertOne("mydb", "users", bson.M{"_id": 1, "name": "Alice", "age": 30})
func (*Client) UpdateMany ¶
UpdateMany updates all documents matching the filter.
Parameters:
- databaseName: Name of the database
- collectionName: Name of the collection
- filter: BSON filter to match documents (e.g., bson.M{"age": bson.M{"$lt": 30}})
- update: Update operations using MongoDB update operators (e.g., bson.D{{"$inc", bson.D{{"age", 1}}}})
Returns error if client is not initialized, connection fails, or update fails.
All matching documents are updated in a single operation. Use update operators like $set, $inc, $push, etc.
Example:
err := client.UpdateMany(
"mydb", "users",
bson.M{"age": bson.M{"$lt": 30}},
bson.D{{"$inc", bson.D{{"age", 1}}}},
)
func (*Client) UpdateOne ¶
UpdateOne updates a single document matching the filter.
Parameters:
- databaseName: Name of the database
- collectionName: Name of the collection
- filter: BSON filter to match the document (e.g., bson.M{"_id": 1})
- update: Update operations using MongoDB update operators (e.g., bson.D{{"$set", bson.D{{"age", 31}}}})
Returns error if client is not initialized, connection fails, or update fails.
Only the first matching document is updated. Use update operators like $set, $inc, $push, etc.
Example:
err := client.UpdateOne(
"mydb", "users",
bson.M{"name": "Alice"},
bson.D{{"$set", bson.D{{"age", 31}}}},
)