aggregations

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package aggregations provides aggregation pipelines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPushdownQuery

func GetPushdownQuery(stagesDocs []any) (match *types.Document, sort *types.Document)

GetPushdownQuery gets pushdown query ($match and $sort) for aggregation.

If the first two stages are either $match, $sort, or a combination of them, we can push them down. In this case, we return the first match and sort statements to pushdown. If $match stage is not present, match is returned as nil. If $sort stage is not present, sort is returned as nil.

func SumNumbers

func SumNumbers(vs ...any) any

SumNumbers accumulate numbers and returns the result of summation. The result has the same type as the input, except when the result cannot be presented accurately. Then int32 is converted to int64, and int64 is converted to float64. It ignores non-number values. For empty `vs`, it returns int32(0). This should only be used for aggregation, aggregation does not return error on overflow.

Types

type Expression

type Expression struct {
	// contains filtered or unexported fields
}

Expression represents a value that needs evaluation.

Expression for access field in document should be prefixed with a dollar sign $ followed by field key. For accessing embedded document or array, a dollar sign $ should be followed by dot notation. Options can be provided to specify how to access fields in embedded array.

func NewExpression

func NewExpression(expression string, opts *commonpath.FindValuesOpts) (*Expression, error)

NewExpression returns Expression from dollar sign $ prefixed string. It can take additional options to specify how to access fields in embedded array.

It returns error if invalid Expression is provided.

func (*Expression) Evaluate

func (e *Expression) Evaluate(doc *types.Document) (any, error)

Evaluate uses Expression to find a field value or an embedded field value of the document and returns found value. If values were found from embedded array, it returns *types.Array containing values.

It returns error if field value was not found. With embedded array field being exception, that case it returns empty array instead of error.

func (*Expression) GetExpressionSuffix

func (e *Expression) GetExpressionSuffix() string

GetExpressionSuffix returns field key of Expression, or for dot notation it returns suffix.

type ExpressionError

type ExpressionError struct {
	// contains filtered or unexported fields
}

ExpressionError describes an error that occurs while evaluating expression.

func (*ExpressionError) Code

Code returns the ExpressionError code.

func (*ExpressionError) Error

func (e *ExpressionError) Error() string

Error implements the error interface.

func (*ExpressionError) Name

func (e *ExpressionError) Name() string

Name returns the value of an expression that produced an error. For an expression `$$$variable`, the invalid value `$variable` is set.

type ExpressionErrorCode

type ExpressionErrorCode int

ExpressionErrorCode represents Expression error code.

const (

	// ErrNotExpression indicates that field is not an expression.
	ErrNotExpression ExpressionErrorCode

	// ErrInvalidExpression indicates that expression is invalid.
	ErrInvalidExpression

	// ErrEmptyFieldPath indicates that field path expression is empty.
	ErrEmptyFieldPath

	// ErrUndefinedVariable indicates that variable name is not defined.
	ErrUndefinedVariable

	// ErrEmptyVariable indicates that variable name is empty.
	ErrEmptyVariable
)

func (ExpressionErrorCode) String

func (i ExpressionErrorCode) String() string

type Stage

type Stage interface {
	// Process applies an aggregate stage on documents from iterator.
	Process(ctx context.Context, iter types.DocumentsIterator, closer *iterator.MultiCloser) (types.DocumentsIterator, error)
}

Stage is a common interface for all aggregation stages.

Directories

Path Synopsis
Package operators provides aggregation operators.
Package operators provides aggregation operators.
accumulators
Package accumulators provides aggregation accumulator operators.
Package accumulators provides aggregation accumulator operators.
Package stages provides aggregation stages.
Package stages provides aggregation stages.
projection
Package projection provides projection for aggregations.
Package projection provides projection for aggregations.

Jump to

Keyboard shortcuts

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