Mongoose for Tinh Tinh is a powerful MongoDB integration package designed specifically for the Tinh Tinh framework. It provides a clean, efficient, and type-safe way to work with MongoDB databases in your Go applications.
- π Simple and intuitive MongoDB integration
- π¦ BSON document handling
- π Automatic model serialization/deserialization
- π― Type-safe query building
- π οΈ Advanced features including:
- Collection operations
- Aggregation pipelines
- Index management
- Transaction support
- Change streams
- GridFS support
To install the package, use:
go get -u github.com/tinh-tinh/mongoose/v2package main
import (
"context"
"github.com/tinh-tinh/mongoose/v2"
)
// User represents your MongoDB document
type User struct {
ID string `bson:"_id,omitempty"`
Name string `bson:"name"`
Email string `bson:"email"`
Age int `bson:"age"`
IsActive bool `bson:"is_active"`
}
func main() {
// Initialize MongoDB connection
client := mongoose.New(&mongoose.Config{
URI: "mongodb://localhost:27017",
Database: "myapp",
})
// Get a collection
collection := client.Collection("users")
// Insert a document
user := User{
Name: "John Doe",
Email: "[email protected]",
Age: 30,
IsActive: true,
}
result, err := collection.InsertOne(context.Background(), user)
if err != nil {
panic(err)
}
}The package supports various configuration options:
type Config struct {
URI string // MongoDB connection URI
Database string // Database name
MaxPoolSize uint64 // Maximum number of connections
MinPoolSize uint64 // Minimum number of connections
ConnectTimeout time.Duration // Connection timeout
MaxConnIdleTime time.Duration // Maximum idle connection time
RetryWrites bool // Enable retry writes
RetryReads bool // Enable retry reads
DirectConnection bool // Use direct connection
}// Find documents
users, err := collection.Find(ctx, bson.M{"age": bson.M{"$gt": 25}})
// Update documents
update := bson.M{"$set": bson.M{"is_active": false}}
result, err := collection.UpdateMany(ctx, bson.M{"age": bson.M{"$lt": 18}}, update)
// Delete documents
result, err := collection.DeleteOne(ctx, bson.M{"email": "[email protected]"})pipeline := mongo.Pipeline{
{{$match: {"age": {"$gt": 25}}}},
{{$group: {"_id": "$city", "count": {"$sum": 1}}}}
}
cursor, err := collection.Aggregate(ctx, pipeline)err := client.UseTransaction(ctx, func(sessCtx mongo.SessionContext) error {
// Perform operations within transaction
_, err := collection.InsertOne(sessCtx, newUser)
if err != nil {
return err
}
_, err = collection.UpdateOne(sessCtx, filter, update)
return err
})-
Connection Management
- Always close connections when done
- Use appropriate pool sizes
- Handle connection errors properly
-
Error Handling
- Check for both operation and connection errors
- Implement proper retry logic
- Log relevant error information
-
Performance Optimization
- Use appropriate indexes
- Implement efficient queries
- Monitor query performance
For detailed documentation and examples, please visit:
We welcome contributions! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
If you encounter any issues or need help, you can:
- Open an issue in the GitHub repository
- Check our documentation
- Join our community discussions