mongo

package module
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2022 License: MIT Imports: 5 Imported by: 0

README

Mongo

Mongo Test codecov GoDoc

A simple wrapper for Go's Mongo Driver

Instillation

Use Go modules

go get github.com/akshaybabloo/mongo/v2

Usage

Unlike the MongoDB driver, this library depends on id and NOT _id. That means you will have to create an index for id field.

See example_test.go

Documentation

Overview

Package mongo is a simple wrapper for MongoDb Driver, this package uses "id" instead of "_id" to find or add a document.

It is important to know that you will have to index id field for optimum performance.

In general, you wouldn't need this package at all, if you rely more on "id" and simple access to MongoDB API than this module will help you.

Example:

import "github.com/akshaybabloo/mongo"

type data struct {
	ID   int    `bson:"id"`
	Name string `bson:"name"`
}

func main() {
	client := NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test")

	testData := data{
		ID:   1,
		Name: "Akshay",
	}

	done, err := client.Add("test_collection", testData)
	if err != nil {
		panic(err)
	}
	print(done.InsertedID)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// ConnectionUrl which connects to MongoDB atlas or local deployment
	ConnectionUrl string

	// DatabaseName with database name
	DatabaseName string
}

Client takes in the

func NewMongoClient

func NewMongoClient(connectionURL string, databaseName string) *Client

NewMongoClient returns Client and it's associated functions

func (*Client) Add

func (connectionDetails *Client) Add(collectionName string, data interface{}) (*mongo.InsertOneResult, error)

Add can be used to add document to MongoDB

Example
type data struct {
	ID   string `bson:"id"`
	Name string `bson:"name"`
}

client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test")

testData := data{
	ID:   "1",
	Name: "Akshay",
}

done, err := client.Add("test_collection", testData)
if err != nil {
	panic(err)
}
fmt.Println("The ID is:", done.InsertedID)
Output:

func (*Client) AddMany

func (connectionDetails *Client) AddMany(collectionName string, data []interface{}) (*mongo.InsertManyResult, error)

AddMany can be used to add multiple documents to MongoDB

Example
type data struct {
	ID   string `bson:"id"`
	Name string `bson:"name"`
}

client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test")

var testData = []interface{}{
	data{
		ID:   "1",
		Name: "Akshay",
	},
	data{
		ID:   "2",
		Name: "Raj",
	},
}

done, err := client.AddMany("test_collection", testData)
if err != nil {
	panic(err)
}
fmt.Println("The ID is:", done.InsertedIDs)
Output:

func (*Client) Collection

func (connectionDetails *Client) Collection(collectionName string) (*mongo.Collection, *mongo.Client, context.Context, error)

Collection returns mongo.Collection

Note: Do not forget to do - defer Client.Disconnect(ctx)

func (*Client) DB

func (connectionDetails *Client) DB() (*mongo.Database, error)

DB returns mongo.Database

func (*Client) Delete

func (connectionDetails *Client) Delete(collectionName string, id string) (*mongo.DeleteResult, error)

Delete deletes a document by ID only.

Example
client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test")

deleted, err := client.Delete("test_collection", "1")
if err != nil {
	panic(err)
}
fmt.Println("Deleted items:", deleted.DeletedCount)
Output:

func (*Client) Get

func (connectionDetails *Client) Get(collectionName string, id string) (*mongo.SingleResult, error)

Get finds one document based on "id" and not "_id"

Example
type data struct {
	ID   int    `bson:"id"`
	Name string `bson:"name"`
}

client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test")

var decodeData data
get, err := client.Get("test_collection", "2")
if err != nil {
	panic("Something went wrong")
}
err = get.Decode(&decodeData)
if err != nil {
	panic("No data found.")
}
fmt.Println(decodeData)
Output:

func (*Client) GetAll

func (connectionDetails *Client) GetAll(collectionName string, id string, result interface{}) error

GetAll finds all documents by "id" and not "_id".

The 'result' parameter needs to be a pointer.

Example
type data struct {
	ID   string `bson:"id"`
	Name string `bson:"name"`
}

client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test")

var testData []data
err := client.GetAll("test_collection", "1", &data{})
if err != nil {
	panic(err)
}
fmt.Println("The ID is:", testData)
Output:

func (*Client) GetAllCustom

func (connectionDetails *Client) GetAllCustom(collectionName string, filter interface{}, result interface{}) error

GetAllCustom finds all documents by filter - bson.M{}, bson.A{}, or bson.D{}.

The 'result' parameter needs to be a pointer.

Example
type data struct {
	ID   string `bson:"id"`
	Name string `bson:"name"`
}

client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test")

var testData []data
err := client.GetAllCustom("test_collection", bson.M{"id": "1"}, &data{})
if err != nil {
	panic(err)
}
fmt.Println("The ID is:", testData)
Output:

func (*Client) GetCustom

func (connectionDetails *Client) GetCustom(collectionName string, filter interface{}) (*mongo.SingleResult, error)

GetCustom finds one document by a filter - bson.M{}, bson.A{}, or bson.D{}

Example
type data struct {
	ID   int    `bson:"id"`
	Name string `bson:"name"`
}

client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test")

var decodeData data
getCustom, err := client.GetCustom("test_collection", bson.M{"id": "2"})
if err != nil {
	panic("No data found.")
}
err = getCustom.Decode(&decodeData)
if err != nil {
	panic("Something went wrong")
}
fmt.Println(decodeData)
Output:

func (*Client) RawClient added in v2.1.0

func (connectionDetails *Client) RawClient() (*mongo.Client, context.Context, error)

RawClient returns mongo.Client

func (*Client) Update

func (connectionDetails *Client) Update(collectionName string, id string, data interface{}) (*mongo.UpdateResult, error)

Update can be used to update values by its ID

Example
type data struct {
	Name string `bson:"name"`
}

client := mongo.NewMongoClient("mongodb://localhost:27017/?retryWrites=true&w=majority", "test")

testData := data{
	Name: "Akshay",
}

updated, err := client.Update("test_collection", "1", testData)
if err != nil {
	panic(err)
}
fmt.Println("Modified items:", updated.ModifiedCount)
Output:

Jump to

Keyboard shortcuts

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