mgo

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 8 Imported by: 0

README

Package mgo

The mgo package provides a MongoDB client for Go applications.

Usage

Import the mgo package into your Go file:

import "github.com/thirathawat/mgo"
Configuration

The mgo package reads its configuration from environment variables. Here are the supported environment variables:

  • ADDRS: A comma-separated list of host:port addresses for the MongoDB servers.
  • NAME: The name of the MongoDB database.
  • AUTH_SOURCE: The name of the MongoDB authentication database.
  • USER: The name of the MongoDB user.
  • PASSWORD: The password for the MongoDB user.

You can use the readConfig function to read the configuration from the environment. It returns a config struct that holds the configuration values.

Creating a MongoDB Client

To create a new MongoDB client and establish a connection, use the New function:

db, cleanup, err := mgo.New()
if err != nil {
    // handle error
}
defer cleanup()

The New function returns a mongo.Database object that represents the MongoDB database. It also returns a cleanup function that should be deferred to close the connection when you're done with the database.

Database Operations

Once you have a mongo.Database object, you can perform various operations on the database, such as inserting, updating, and querying documents. Refer to the MongoDB Go Driver documentation for more information on working with the MongoDB database.

Example

Here's an example of how to use the mgo package:

package main

import (
    "github.com/tirathawat/mgo"
)

func main() {
    db, cleanup, err := mgo.New()
    if err != nil {
        // handle error
    }
    defer cleanup()

    // Perform database operations...
}

Conclusion

The mgo package provides a convenient way to connect to a MongoDB database and perform database operations in your Go applications. It simplifies the process of configuring the client and establishing a connection, allowing you to focus on building your application's functionality.

Documentation

Overview

Package mgo provides a MongoDB client.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() (db *mongo.Database, cleanup func(), err error)

New creates a new MongoDB client and establishes a connection.

Types

type Collecter added in v1.1.0

type Collecter[T any] interface {
	// InsertOne insert one document.
	InsertOne(ctx context.Context, model T) error

	// InsertMany insert many documents.
	InsertMany(ctx context.Context, models []T) error

	// FindOne find one document.
	FindOne(ctx context.Context, options ...Option) (*T, error)

	// FindMany find many documents.
	FindMany(ctx context.Context, options ...Option) ([]T, error)

	// UpdateOne update one document.
	UpdateOne(ctx context.Context, options ...Option) error

	// UpdateMany update many documents.
	UpdateMany(ctx context.Context, options ...Option) error

	// DeleteOne delete one document.
	DeleteOne(ctx context.Context, options ...Option) error

	// DeleteMany delete many documents.
	DeleteMany(ctx context.Context, options ...Option) error

	// SoftDeleteOne soft delete one document by setting deleted_at.
	SoftDeleteOne(ctx context.Context, options ...Option) error

	// SoftDeleteMany soft delete many documents by setting deleted_at.
	SoftDeleteMany(ctx context.Context, options ...Option) error

	// Count count documents.
	Count(ctx context.Context, options ...Option) (int64, error)

	// Aggregate aggregate documents.
	Aggregate(ctx context.Context, options ...Option) ([]T, error)
}

Collecter is a interface for mongo collection.

func NewCollection added in v1.1.0

func NewCollection[T entiter](c *mongo.Collection) Collecter[T]

NewCollection is a function to create a new collection.

type Entity added in v1.2.0

type Entity struct {
	// ID is a id of entity.
	ID primitive.ObjectID `bson:"_id"`

	// CreatedAt is a time when entity is created.
	CreatedAt time.Time `bson:"created_at"`

	// UpdatedAt is a time when entity is updated.
	UpdatedAt time.Time `bson:"update_at"`

	// DeletedAt is a time when entity is deleted.
	// If entity is not deleted, DeletedAt is nil.
	DeletedAt *time.Time `bson:"deleted_at"`
}

Entity is a base entity.

type Option added in v1.1.0

type Option func(*option)

Option is a function to set option.

Jump to

Keyboard shortcuts

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