gomongo

package module
v0.0.0-...-983e2a1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: GPL-3.0 Imports: 10 Imported by: 0

README

GoLang Simplified MongoDB Library

This library provides a simplified interface for common operations in MongoDB, such as retrieving data, deleting items, and searching within collections. It aims to streamline and simplify the usage of these core functions, reducing the complexity and boilerplate code typically associated with such operations.

Supports the following function:

  • Get
  • Add
  • Delete
  • DeleteMany
  • List
  • UpdateOne
  • UpdateMany

Installation

To include this library in your project, you can simply use Go modules:

go get -u github.com/yottab-io/gomongo

Features

  • Simplified API: The library offers a simplified API for performing common data operations, making it easier and more intuitive to work with.
  • Connection Management: The library provides two key functions for connection management:
  • Connect: This function establishes a connection with the data source and returns a handle to the connection.
  • CloseConnection: This function safely closes the connection when exiting the program, ensuring that resources are released properly.

Usage

To use the library, simply import it into your Go project and leverage the simplified functions for efficient data manipulation. Below is an example of how to get started:

import (
  "github.com/yottab-io/gomongo"
  "go.mongodb.org/mongo-driver/bson"
  "go.mongodb.org/mongo-driver/bson/primitive"
)

// Example Update Document
filter := bson.D{{"company", "test"}, {"domain", "example.com"}}
update := bson.D{{"$set", bson.D{{"status", "active"}}}}
err := mongodb.UpdateOne("DBName", "CollectionName", filter, update)
if err != nil {
    // Handle error
}

// Example Get Document
type User struct{
	ID      primitive.ObjectID `json:"-"       bson:"_id,omitempty"`
	Company string             `json:"company" bson:"company"`
	Domain  string             `json:"domain"  bson:"domain"`
}
userInfo := new(User)
filter := bson.D{{"company", "test"}}
err := mongodb.Get("DBName", "CollectionName", filter, userInfo)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadReq      = errors.New("MongoDB Bad request")
	ClearFilter    = bson.D{}
	ErrNoDocuments = mongo.ErrNoDocuments
)

Functions

func Add

func Add(db, col string, obj interface{}, opts ...*options.InsertOneOptions) (id primitive.ObjectID, err error)

func Aggregate

func Aggregate(db, col string, filter interface{}, pointerToSliceResults interface{}, opts ...*options.AggregateOptions) error

func ClientClose

func ClientClose()

ClientClose close the client

func Delete

func Delete(db, col string, filter interface{}, opts ...*options.DeleteOptions) error

func DeleteMany

func DeleteMany(db, col string, filter interface{}, opts ...*options.DeleteOptions) error

func Get

func Get(db, col string, filter interface{}, result interface{}, opts ...*options.FindOneOptions) error

func InnitConnection

func InnitConnection()

func List

func List(db, col string, filter interface{}, pointerToSliceResults interface{}, pageNum, limitNum int, opts ...*options.FindOptions) error

type of results must be array of struct

func StructToDoc

func StructToDoc(v interface{}) (doc *bson.D, err error)

func StructToUnorderedDoc

func StructToUnorderedDoc(v interface{}) (doc *bson.M, err error)

func UpdateMany

func UpdateMany(db, col string, filter, update interface{}, opts ...*options.UpdateOptions) error

func UpdateOne

func UpdateOne(db, col string, filter, update interface{}, opts ...*options.UpdateOptions) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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