Skip to content

A Go package providing a generic, extensible MongoDB repository with advanced CRUD operations, error handling, and flexible querying capabilities.

License

Notifications You must be signed in to change notification settings

dmitrymomot/mongo-repository

Repository files navigation

mongo-repository

GitHub tag (latest SemVer) Go Reference License

Tests CodeQL Analysis GolangCI Lint Go Report Card

This Go package provides a generic, extensible MongoDB repository with advanced CRUD operations, flexible querying capabilities, and support for common MongoDB patterns. It is designed to simplify the interaction with MongoDB using Go's standard library and the official MongoDB Go driver.

Features

  • Generic CRUD operations for MongoDB documents.
  • Query helpers for complex filters and full-text search.
  • Support for finding documents by multiple IDs.
  • Custom index creation with various index options.
  • Batch update capabilities.

Installation

To use this MongoDB repository package, you need to have Go installed on your machine. The package can be installed using the following Go command:

go get -u github.com/dmitrymomot/mongo-repository

Usage

Import the package into your Go file:

import "github.com/dmitrymomot/mongo-repository"

Basic CRUD Operations

Here's a quick example of how you can use the repository for CRUD operations:

func main() {
    // Initialize your MongoDB client and context
    // ...

    // Create a new repository for your model
    userRepo := repository.NewMongoRepository[User](db, "users")

    // Use the repository for various operations
    // ...
}

Advanced Querying

The package includes a filter builder to create complex queries easily:

func findUsers(repo repository.Repository[User]) {
    ctx := context.TODO()
    filter := repository.And(
        repository.Gt("age", 30),
        repository.Eq("status", "active"),
    )
    users, err := repo.FindManyByFilter(ctx, 0, 10, filter)
    // Handle err and work with users
}

Full-Text Search

The package includes a full-text search builder to create text queries easily. The text search query uses the MongoDB text search feature.

How to use it - see the tests: full_text_search_test.go.

TTL Index

To create an index with a Time-To-Live (TTL) in MongoDB, which automatically deletes documents after a certain amount of time, you need to specify the TTL when creating the index. MongoDB uses a special background task that runs periodically to remove expired documents.

Creating a TTL Index in Your Collection

When you want to create an index with a TTL, specify the field and use the TTL option. This field typically stores the creation time of the document and should be of BSON date type.

// Create an index with a TTL of 24 hours
err := repo.CreateIndex(context.TODO(), "createdAt", mongorepository.TTL(24*time.Hour))

Document Structure

Ensure your documents have a field (like createdAt) that stores the time when the document was created. This field is used by MongoDB to determine if a document is expired.

type YourDocumentType struct {
    ID        primitive.ObjectID `bson:"_id,omitempty"`
    CreatedAt time.Time          `bson:"createdAt"`
    // Other fields...
}

Notes

  • MongoDB runs a background task every 60 seconds to remove expired documents, so there may be a slight delay before documents are actually deleted.
  • This approach is commonly used for data that needs to be retained only for a specific duration, such as logs, temporary data, or session information.

Contributing

Contributions to the mongo-repository package are welcome! Here are some ways you can contribute:

  • Reporting bugs
  • Additional tests cases
  • Suggesting enhancements
  • Submitting pull requests
  • Sharing the love by telling others about this project

License

This project is licensed under the Apache 2.0 - see the LICENSE file for details.

About

A Go package providing a generic, extensible MongoDB repository with advanced CRUD operations, error handling, and flexible querying capabilities.

Resources

License

Stars

Watchers

Forks

Packages