mongo

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 11 Imported by: 4

README

MongoDB module

This repository contains code to deal with MongoDB operations used by FOXDEN/CHESS services. It covers read/write/update/delete APIs.

Documentation

Index

Constants

View Source
const (
	ServerError
	DBError
	QueryError
	ParserError
	ValidationError
)
View Source
const (
	ServerErrorName     = "Server error"
	DBErrorName         = "MongoDB error"
	QueryErrorName      = "Server query error"
	ParserErrorName     = "Server parser error"
	ValidationErrorName = "Server validation error"
)

ServerErrorName and others provides human based definition of the error

Variables

This section is empty.

Functions

func Count

func Count(dbname, collname string, spec map[string]any) int

Count gets number records from MongoDB

func Distinct added in v0.4.2

func Distinct(dbname, collname, field string) ([]any, error)

Distinct gets number records from MongoDB

func ErrorRecord

func ErrorRecord(msg, etype string, ecode int) map[string]any

ErrorRecord provides error record

func Get

func Get(dbname, collname string, spec map[string]any, idx, limit int) []map[string]any

Get records from MongoDB

func GetInt64Value

func GetInt64Value(rec map[string]any, key string) (int64, error)

GetInt64Value function to get int value from record for given key

func GetIntValue

func GetIntValue(rec map[string]any, key string) (int, error)

GetIntValue function to get int value from record for given key

func GetSingleStringValue

func GetSingleStringValue(rec map[string]any, key string) (string, error)

GetSingleStringValue function to get string value from record for given key

func GetSorted

func GetSorted(dbname, collname string, spec map[string]any, skeys []string, sortOrder, idx, limit int) []map[string]any

GetSorted records from MongoDB sorted by given key with specific order

func GetStringValue

func GetStringValue(rec map[string]any, key string) (string, error)

GetStringValue function to get string value from record for given key

func GetValue

func GetValue(rec map[string]any, key string) interface{}

GetValue function to get int value from record for given key

func InitMongoDB

func InitMongoDB(uri string)

InitMongoDB initializes MongoDB connection object

func Insert

func Insert(dbname, collname string, records []map[string]any)

Insert records into MongoDB

func InsertAny added in v0.1.0

func InsertAny(dbname, collname string, records []any)

InsertAny insert records into MongoDB

func InsertRecord added in v0.3.2

func InsertRecord(dbname, collname string, rec map[string]any) error

InsertRecord insert record with given spec to MongoDB

func MapKeys

func MapKeys(rec map[string]any) []string

MapKeys helper function to return keys from a map

func Remove

func Remove(dbname, collname string, spec map[string]any) error

Remove records from MongoDB

func Update

func Update(dbname, collname string, spec, newdata map[string]any)

Update inplace for given spec

func Upsert

func Upsert(dbname, collname, attr string, records []map[string]any) error

Upsert records into MongoDB

func UpsertAny added in v0.1.0

func UpsertAny(dbname, collname string, records []any) error

UpsertAny upsert records into MongoDB

func UpsertRecord added in v0.1.0

func UpsertRecord(dbname, collname string, spec, rec map[string]any) error

UpsertRecord insert record with given spec to MongoDB

Types

type Connection

type Connection struct {
	Client *mongo.Client
	URI    string
}

Connection defines connection to MongoDB

var Mongo Connection

Mongo holds MongoDB connection

func (*Connection) Connect

func (m *Connection) Connect() *mongo.Client

Connect provides connection to MongoDB

type MongoDB added in v0.5.9

type MongoDB struct {
}

MongoDB represents MongoDB interface with MongoDB backend

func (*MongoDB) Count added in v0.5.9

func (d *MongoDB) Count(dbname, collname string, spec map[string]any) int

Count returns total number of records within database/collection and given spec

func (*MongoDB) Distinct added in v0.5.9

func (d *MongoDB) Distinct(dbname, collname, field string) ([]any, error)

Distinct returns distinct collection of records

func (*MongoDB) Get added in v0.5.9

func (d *MongoDB) Get(dbname, collname string, spec map[string]any, idx, limit int) []map[string]any

Get fetches data from underlying database/collection

func (*MongoDB) GetSorted added in v0.5.9

func (d *MongoDB) GetSorted(dbname, collname string, spec map[string]any, skeys []string, sortOrder, idx, limit int) []map[string]any

GetSorted returns sorted records from given database/collection using provided spec, sorted keys, order and limits

func (*MongoDB) InitDB added in v0.5.9

func (d *MongoDB) InitDB(uri string)

InitDB initializes MongoDB backend

func (*MongoDB) Insert added in v0.5.9

func (d *MongoDB) Insert(dbname, collname string, records []map[string]any)

Insert inserts records into provided database/collection

func (*MongoDB) InsertRecord added in v0.5.9

func (d *MongoDB) InsertRecord(dbname, collname string, rec map[string]any) error

InsertRecord inserts single record into given database/collection

func (*MongoDB) Remove added in v0.5.9

func (d *MongoDB) Remove(dbname, collname string, spec map[string]any) error

Remove deletes records in given database/collection using given spec

func (*MongoDB) Update added in v0.5.9

func (d *MongoDB) Update(dbname, collname string, spec, newdata map[string]any)

Update updates data into given database/collection

func (*MongoDB) Upsert added in v0.5.9

func (d *MongoDB) Upsert(dbname, collname, attr string, records []map[string]any) error

Upsert inserts records into provided database/collection and attribute

Jump to

Keyboard shortcuts

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