db

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

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

Go to latest
Published: Dec 20, 2019 License: MIT Imports: 7 Imported by: 0

README

GoDoc GoReportCard

MongoDB Go Driver Wrapper

This package reduces boilerplate when using MongoDB by handling contexts and cursor iteration for you.

Install

Use the package by running go get -u github.com/jackyzha0/monGo-driver-wrapper/ and adding the import to the top of your file

db "github.com/jackyzha0/monGo-driver-wrapper"

Usage

Instantiate Connection

Begin by creation a connection to a MongoDB collection by specifying the URL, database name, and collection name.

// Create new connection to the `users` collection in the `exampleDB` database on the local instance
var Collection = db.New("mongodb://localhost:27017", "exampleDB", "users")
Insert a document
// First, create a struct to represent your data
type Doc struct {
	Name    string `json:"name"`
	Surname string `json:"surname"`
}

// Create a new document
john := Doc{"john", "smith"}

// Insert said document into the collection, and get the insertID assigned by MongoDB
err, insertID = Collection.InsertOne(john)
Insert multiple documents

Assuming you've already designed a struct to represent your data and made a connection,

// Define some structs
john := Doc{"john", "smith"}
betty := Doc{"betty", "hansen"}

// Make a slice out of them
sl := []interface{}{john, betty}
err, insertIDs := Collection.InsertMany(sl)
Find a document
// Define a filter to search for. Below is a query to search for all documents with the name `bob`
filter := bson.D{{"name", "bob"}}

// Define the struct that you wish to unmarshall the response into. If the call is successful, res will take on the value of the result.
var res Doc
err := Collection.FindOne(filter, &res)
Find multiple documents
filter := bson.D{{"surname", "joe"}}

// Make sure that when searching for multiple documents, you use a slice of interfaces
var res []interface{}
err := Collection.FindMany(filter, &res)

// Then, create a new slice of the desired type of struct
var val []Doc

// This part unmarshalls the slice of interfaces into the desired slice of structs.
for _, el := range got {
  var d Doc
  bsonBytes, _ := bson.Marshal(el)
  bson.Unmarshal(bsonBytes, &d)
  val = append(val, d)
}
Update a document
// Define a filter to select which types of documents should be updated
update_filter := bson.D{{"name", "rebecca"}}

// Define how to update said documents
update := bson.D{{"$set", bson.D{{"surname", "o'connor"}}}}

// Returns number of documents matched and modified.
err, match, mod := Collection.UpdateOne(update_filter, update)
Update multiple documents
// Increment the points of anyone on team red by one
update_filter := bson.D{{"team", "red"}}
update := bson.D{{"$inc", bson.D{{"points", 1}}}}
err, match, mod := Collection.UpdateMany(update_filter, update)
Delete a document
// Delete a single document with `name: albert`
filter := bson.D{{"name", "albert"}}
err = TestCollection.DeleteOne(filter)
Delete multiple documents
// Delete all documents with `name: albert`
filter := bson.D{{"name", "albert"}}
err = TestCollection.DeleteOne(filter)

Testing / Development

Ensure that you have a mongod instance running locally on your machine. Run the sanity check tests by doing

go test -v

This will create a new Database called exampleDB and collection named test which it will delete after.

Documentation

Overview

Package db provides helper functions that make interfacing with the MongoDB Go driver library easier

Index

Constants

View Source
const OperationTimeOut = 5

OperationTimeOut is the time limit for context before cancelling

Variables

View Source
var Client *mongo.Client

Client holds the reference to the underlying MongoDB client

Ctx holds the current context

Functions

This section is empty.

Types

type CnctConnection

type CnctConnection struct {
	Collection *mongo.Collection
}

CnctConnection is the wrapper for Mongo Collection

func New

func New(uri, db, cnct string) (c CnctConnection)

New creates a new connection to Mongo Collection

func (CnctConnection) DeleteMany

func (db CnctConnection) DeleteMany(filter bson.D) error

DeleteMany deletes all documents that match the bson.D filter

func (CnctConnection) DeleteOne

func (db CnctConnection) DeleteOne(filter bson.D) error

DeleteOne deletes single document that match the bson.D filter

func (CnctConnection) Drop

func (db CnctConnection) Drop() error

Drop drops the current CnctConnection (collection)

func (CnctConnection) FindMany

func (db CnctConnection) FindMany(filter bson.D, res *[]interface{}) error

FindMany iterates cursor of all docs matching filter and fills res with unmarshalled documents.

func (CnctConnection) FindOne

func (db CnctConnection) FindOne(filter bson.D, res interface{}) error

FindOne finds first document that satisfies filter and fills res with the unmarshalled document.

func (CnctConnection) InsertMany

func (db CnctConnection) InsertMany(new []interface{}) (interface{}, error)

InsertMany takes a slice of structs, inserts them into the database, and returns list of inserted IDs

func (CnctConnection) InsertOne

func (db CnctConnection) InsertOne(new interface{}) (interface{}, error)

InsertOne inserts a single struct as a document into the database and returns its ID. Returns inserted ID

func (CnctConnection) UpdateMany

func (db CnctConnection) UpdateMany(filter, update bson.D) (int64, int64, error)

UpdateMany updates all documents matching the filter by applying the update query on it. Returns number of documents matched and modified.

func (CnctConnection) UpdateOne

func (db CnctConnection) UpdateOne(filter, update bson.D) (int64, int64, error)

UpdateOne updates single document matching filter and applies update to it. Returns number of documents matched and modified. Should always be either 0 or 1.

Jump to

Keyboard shortcuts

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