mgo

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2023 License: MIT Imports: 7 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 ...SetOption) (*T, error)

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

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

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

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

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

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

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

Collecter is a interface for mongo collection

func NewCollection added in v1.1.0

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

NewCollection is a function to create a new collection

type Option added in v1.1.0

type Option struct {
	// Filter is a filter to find
	Filter bson.M

	// Update is a update to update
	Update bson.M

	// Sort is a sort to sort
	Sort bson.M

	// Projection is a projection to projection
	Projection bson.M

	// Pipeline is a pipeline to aggregate
	Pipeline []bson.M

	// Skip is a skip to skip
	Skip *int64

	// Limit is a limit to limit
	Limit *int64
}

Filter is a function to set filter

type SetOption added in v1.1.0

type SetOption func(*Option)

SetOption is a function to set option

Jump to

Keyboard shortcuts

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