mongo

package
v2.4.6 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2022 License: MIT Imports: 11 Imported by: 0

README

shared | mongo

This package utilizes the official mongo driver making it simpler to configure and connect to a mongo instance for reading and writing.

How To Use

package main

import (
	"context"

	"github.com/jobaldw/shared/v2/config"
	"github.com/jobaldw/shared/v2/mongo"
)

func main() {

	// Use the shared config.Mongo() struct for mongo client configurations.
	// NOTE: If you use the URI, you will need to user and password url encoding. 
	// Using the Username and Password options will handle the encoding for you.
	conf := config.Mongo{
		Database: "dbName",
		URI:      "bW9uZ28rc3ZyOi8vPHlvdXJNb25nb1VSST4",
		Collections: map[string]string{
			"key1": "value1",
		},
	}

	// Create new mongo client.
	mongoClient, err := mongo.New(conf)
	if err != nil {
		// handle error
	}

	// The GetCollection() gives you the ability to interact with the collection's 
	// CRUD operations.
	key1 := mongoClient.GetCollection("key1")
	key1.FindOne(context.Background(), nil)
}

Documentation

Overview

Package mongo implements a Mongo instance that connects to a database and allows interactions with the datasource.

All functions that require a context to be passed should be given one from the service handler request to correctly handle cancellations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoCollections = errors.New("no configured collections") // no collection to read from or write to
	ErrNoDatabase    = errors.New("no database found")         // no database has been connected
	ErrNoDocuments   = errors.New("no documents in result")    // no mongo documents are in the result set
)

Functions

This section is empty.

Types

type Mongo

type Mongo struct {
	// host or host:port of the mongo uri
	Host string

	// mongo database name
	Name string

	// username/password information for uri authentication
	User string

	// handles to a MongoDB collection
	Collections map[string]*mongo.Collection

	// handle to a MongoDB database
	Database *mongo.Database

	// representation of a parsed URL (technically, a URI reference)
	URI *url.URL
}

Holds the configurations for a mongo connection using the official mongo driver package.

func New

func New(conf config.Mongo) (*Mongo, error)

New implements a new mongo object that connects to a database and creates a handle for mongo collections.

func (*Mongo) GetCollection

func (m *Mongo) GetCollection(key string) *mongo.Collection

GetCollection retrieves a mongo collection object.

func (*Mongo) Ping

func (m *Mongo) Ping() (err error)

Ping test the mongo database connection.

func (*Mongo) PingWithContext

func (m *Mongo) PingWithContext(ctx context.Context) (err error)

PingWithContext test the mongo database connection with any passed in context.Context().

Jump to

Keyboard shortcuts

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