bsontools

package
v0.0.0-...-ed1b477 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package bsontools exposes various BSON-related tools.

For other MongoDB-related tools, see the mongotools package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareBinaries

func CompareBinaries(a, b bson.RawValue) (int, error)

CompareBinaries compares two BSON binary strings per BSON sort order.

func CompareInt64s

func CompareInt64s(a, b bson.RawValue) (int, error)

CompareInt64s compares two BSON int64s.

func CompareStrings

func CompareStrings(a, b bson.RawValue) (int, error)

CompareStrings compares two BSON strings.

func CountRawElements

func CountRawElements[D ~[]byte](doc D) (int, error)

CountRawElements returns a count of the fields in the given BSON document.

func GetComparableTypes

func GetComparableTypes() mapset.Set[bson.Type]

GetComparableTypes returns a Set of BSON types that this library can compare.

func MarshalA

func MarshalA[T ~[]byte](buf T, a bson.A) (bson.RawArray, error)

MarshalA is like MarshalD but for a bson.A.

func MarshalD

func MarshalD[T ~[]byte](buf T, d bson.D) (bson.Raw, error)

MarshalD marshals a bson.D to BSON, yielding the same result as bson.Marshal(). This avoids reflection, though, which significantly reduces CPU load. It can also marshal to a preexisting buffer, which lets you minimize GC churn.

func RawElements

func RawElements[D ~[]byte](doc D) iter.Seq2[bson.RawElement, error]

RawElements returns an iterator over a Raw’s elements.

If the iterator returns an error but the caller continues iterating, a panic will ensue.

func RawLookup

func RawLookup[T unmarshalTargets, D ~[]byte](in D, pointer ...string) (T, error)

RawLookup extracts & unmarshals a referent value from a BSON document. It’s like bson.Raw.LookupErr combined with RawValueTo.

func RawValueTo

func RawValueTo[T unmarshalTargets](in bson.RawValue) (T, error)

RawValueTo is a bit like bson.UnmarshalValue, but it’s much faster because it avoids reflection. The downside is that only certain types are supported.

This usually enforces strict numeric type equivalence. For example, it won’t coerce a float to an int64. If Go’s int type is the target, either int32 or int64 is acceptable.

Example usage:

str, err := RawValueTo[string](rv)

func RawValueToBinary

func RawValueToBinary(in bson.RawValue) (bson.Binary, error)

RawValueToBinary is a more efficient RawValueTo[bson.Binary].

func RawValueToInt

func RawValueToInt(in bson.RawValue) (int, error)

RawValueToInt is a more efficient RawValueTo[int].

func RawValueToInt64

func RawValueToInt64(in bson.RawValue) (int64, error)

RawValueToInt64 is a more efficient RawValueTo[int64].

func RawValueToString

func RawValueToString(in bson.RawValue) (string, error)

RawValueToString is a more efficient RawValueTo[string].

func RawValueToStringBytes

func RawValueToStringBytes(in bson.RawValue) ([]byte, error)

RawValueToStringBytes is like RawValueToString but returns the raw buffer rather than allocating a new string.

func RawValueToTimestamp

func RawValueToTimestamp(in bson.RawValue) (bson.Timestamp, error)

RawValueToTimestamp is a more efficient RawValueTo[bson.Timestamp].

func RemoveFromRaw

func RemoveFromRaw[T ~[]byte](raw T, pointer ...string) (T, bool, error)

RemoveFromRaw is like ReplaceInRaw, but it removes the element.

Example usage (replaces /role/title):

rawDoc, found, err = RemoveFromRaw(rawDoc, "role", "title")

func ReplaceInRaw

func ReplaceInRaw[T ~[]byte](raw T, newValue bson.RawValue, pointer ...string) (T, bool, error)

ReplaceInRaw “surgically” replaces one value in a BSON document with another. Its returned bool indicates whether the value was found.

If any nonfinal node in the pointer is a scalar value (i.e., neither an array nor embedded document), a PointerTooDeepError is returned.

Example usage (replaces /role/title):

rawDoc, found, err = ReplaceInRaw(rawDoc, newRoleTitle, "role", "title")

func SortFields

func SortFields(in bson.Raw) error

SortFields sorts a BSON document’s fields recursively. It modifies the provided bson.Raw directly.

func ToRawValue

func ToRawValue[T alwaysMarshalableTypes](in T) bson.RawValue

ToRawValue is a bit like bson.MarshalValue, but: - It’s faster since it avoids reflection. - It always succeeds since it only accepts certain known types.

func UnmarshalArray

func UnmarshalArray(raw bson.RawArray) (bson.A, error)

UnmarshalArray is like UnmarshalRaw but for an array.

func UnmarshalToD

func UnmarshalToD[D ~[]byte](raw D) (bson.D, error)

UnmarshalToD mimics bson.Unmarshal to a bson.D.

Types

type PointerTooDeepError

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

PointerTooDeepError is like POSIX’s ENOTDIR error: it indicates that a nonfinal element in a document pointer is a scalar value.

func (PointerTooDeepError) Error

func (pe PointerTooDeepError) Error() string

Jump to

Keyboard shortcuts

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