gomongoclient

package module
v0.0.0-...-b31da40 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2021 License: MIT Imports: 8 Imported by: 0

README

gomongoclient

  • gomongoclient implements and simplify the mongodb-driver usage
Under Construction
  • Feel free to submit issues, requests and ideas.
Todos
  • More examples
  • TTL index
  • Create indexes
  • Connection pool
Usage example

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/laercioamatuzzi/gomongoclient"
	"go.mongodb.org/mongo-driver/bson"
)

func main() {
	mongo := gomongoclient.MongoDB{
		Host:     "127.0.0.1",
		User:     "localhost",
		Password: "example",
		Port:     27017,
		Database: "root",
	}

	// Connect to the mongo Database.
	err := mongo.Init()

	if err != nil {
		log.Fatal(err)
	}

	// Closing the connection instance.
	defer mongo.Done()

	// Insert
	collection := "Test"
	query := bson.M{"key": "value"}
	err = mongo.Insert(collection, query)

	if err != nil {
		log.Fatal(err)
	}

	// Select
	filter := bson.M{}
	sort := bson.M{}

	cursor, err := mongo.FindAndConvert("Test", filter, sort, 50)

	if err != nil {
		log.Fatal(err)
	}

	type Test struct {
		ObjectID string `bson:"_id"`
		Value    string `bson:"value"`
	}

	for cursor.Next(context.TODO()) {

		var t Test
		cursor.Decode(&t)

		fmt.Println(t)
	}
}

Documentation

Overview

gomongoclient implements and simplify the mongodb-driver usage

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MongoDB

type MongoDB struct {
	Host     string
	Port     int
	User     string
	Password string
	Database string
	// contains filtered or unexported fields
}

func (*MongoDB) Done

func (m *MongoDB) Done() (err error)

Done close all connections to this stance and free all the cache

func (*MongoDB) Find

func (m *MongoDB) Find(collection string, query interface{}, limit int64) (result []bson.M, err error)

Find the values from given query

func (*MongoDB) FindAndConvert

func (m *MongoDB) FindAndConvert(collection string, query interface{}, sort interface{}, limit int64) (cur *mongo.Cursor, err error)

FindAndConvert return the mongo cursor to use in struct conversion

func (*MongoDB) Init

func (m *MongoDB) Init() (err error)

Init the basic connection

func (*MongoDB) Insert

func (m *MongoDB) Insert(collection string, query interface{}) error

Insert a new Value to the Collection

func (*MongoDB) InsertMany

func (m *MongoDB) InsertMany(collection string, query []interface{}, ordered bool) error

InsertMany values to the given collection

func (*MongoDB) Ping

func (m *MongoDB) Ping(connectOnFailure bool) (err error)

Ping tests the connection with the mongo Server

func (*MongoDB) Test

func (m *MongoDB) Test() string

func (*MongoDB) Update

func (m *MongoDB) Update(collection string, filter bson.M, update bson.M)

func (*MongoDB) Upsert

func (m *MongoDB) Upsert(collection string, values bson.M, filter bson.M) error

func (*MongoDB) UpsertMany

func (m *MongoDB) UpsertMany(collection string, query interface{}, filter interface{})

UpsertMany records on the mongo database

Jump to

Keyboard shortcuts

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