doc

package
v0.0.0-...-d078b10 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 6 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// PermutationThreshold limits the complexity of Unwound maps to the
	// given array length.
	PermutationThreshold = 1024
)

Variables

View Source
var (
	// ErrPermutations is returned if the map contains arrays with a combined
	// number of permutations than the threshold.
	ErrPermutations = fmt.Errorf(
		"object complexity (array permutations) exceeded threshold")
)

Functions

func DocumentFromStruct

func DocumentFromStruct(
	sct interface{},
	appendElements ...bson.E,
) (doc bson.D)

DocumentFromStruct creates a bson document from a struct in the order of the underlying data structure. Additional fields can be appended to the struct with the appendElements, these fields will be added at the end of the document.

func FlattenDocument

func FlattenDocument(
	mapping interface{}, options ...*FlattenOptions,
) (doc bson.D, err error)

FlattenDocument consumes a struct or map and returns a flattened BSON document (bson.D) where embedded structs and maps are flattened into the root level of the map and field-names are concatenated by dots ('.'). This function respects the "omitempty" bson tags such that it's possible to create queries from structs; the resulting document respects the struct declaration order with a depth first expansion of embedded fields. Please note, however, that there are no ordering guarantees on map-types.

type Foo struct {
    Bar: map[string]string{
        "baz": "foo"
    },  `bson:"bar"`
}

Becomes:

bson.D{
  {Key: "bar.baz", Value: "foo"}
}

func MarshallBSONOrDocumentFromStruct

func MarshallBSONOrDocumentFromStruct(
	sct interface{},
	appendElements ...bson.E,
) (doc bson.D)

MarshallBSONOrDocumentFromStruct marshals a structure to BSON if it implements the bson.Marshaler interface, otherwise invokes DocumentFromStruct on it.

func UnwindMap

func UnwindMap(
	in interface{},
) ([]bson.D, error)

UnwindMap takes a string map and perform mongodb $unwind aggregation operation on all array entries. Don't return a map - but a sorted list of tuples; mongo indices are sensitive to order. Example:

{"foo": ["1", "2"], "bar": "3"} becomes
[ {{"foo", "1", "bar", "3"}}, {{"foo": "2", "bar": "3"}} ]

Types

type FlattenOptions

type FlattenOptions struct {
	// Transform provides an option for transforming key/value pairs in
	// the flattened array. The input contains the values that would
	// otherwise be added to the document. This can be useful for
	// transforming query containing arrays to add an $in operator.
	Transform func(key string, elem interface{}) (string, interface{})
}

func NewFlattenOptions

func NewFlattenOptions() *FlattenOptions

func (*FlattenOptions) SetTransform

func (opts *FlattenOptions) SetTransform(
	transform func(key string, elem interface{}) (string, interface{}),
) *FlattenOptions

Jump to

Keyboard shortcuts

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