mongo

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: MIT Imports: 11 Imported by: 0

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 bson.M) int

Count 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 bson.M, 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 bson.M, skeys []string) []map[string]any

GetSorted records from MongoDB sorted by given key

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 bson.M) 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 bson.M) error

Remove records from MongoDB

func Update

func Update(dbname, collname string, spec, newdata bson.M)

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 bson.M, rec bson.M) 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

Jump to

Keyboard shortcuts

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