Documentation ¶
Index ¶
- Constants
- Variables
- func DocumentFromStruct(sct interface{}, appendElements ...bson.E) (doc bson.D)
- func FlattenDocument(mapping interface{}, options ...*FlattenOptions) (doc bson.D, err error)
- func MarshallBSONOrDocumentFromStruct(sct interface{}, appendElements ...bson.E) (doc bson.D)
- func UnwindMap(in interface{}) ([]bson.D, error)
- type FlattenOptions
Constants ¶
const ( // PermutationThreshold limits the complexity of Unwound maps to the // given array length. PermutationThreshold = 1024 )
Variables ¶
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 ¶
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 ¶
MarshallBSONOrDocumentFromStruct marshals a structure to BSON if it implements the bson.Marshaler interface, otherwise invokes DocumentFromStruct on it.
func UnwindMap ¶
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