bsonutil

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package bsonutil provides utilities for processing BSON data.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSuchField = errors.New("no such field")

Functions

func Bson2Float64

func Bson2Float64(data interface{}) (float64, bool)

func ConvertBSONValueToLegacyExtJSON

func ConvertBSONValueToLegacyExtJSON(x interface{}) (interface{}, error)

ConvertBSONValueToLegacyExtJSON walks through a document or an array and converts any BSON value to its corresponding extended JSON type. It returns the converted JSON document and any error encountered.

func ConvertLegacyExtJSONDocumentToBSON

func ConvertLegacyExtJSONDocumentToBSON(doc map[string]interface{}) error

ConvertLegacyExtJSONDocumentToBSON iterates through the document map and converts JSON values to their corresponding BSON values. It also replaces any extended JSON type value (e.g. $date) with the corresponding BSON type

func ConvertLegacyExtJSONValueToBSON

func ConvertLegacyExtJSONValueToBSON(x interface{}) (interface{}, error)

ConvertLegacyExtJSONValueToBSON walks through a document or an array and replaces any extended JSON value with its corresponding BSON type.

func ConvertLegacyIndexKeys

func ConvertLegacyIndexKeys(indexKey bson.D, ns string)

ConvertLegacyIndexKeys transforms the values of index definitions pre 3.4 into the stricter index definitions of 3.4+. Prior to 3.4, any value in an index key that isn't a negative number or that isn't a string is treated as int32(1). The one exception is an empty string is treated as int32(1). All other strings that aren't one of ["2d", "geoHaystack", "2dsphere", "hashed", "text", ""] will cause the index build to fail. See TOOLS-2412 for more information.

This function logs the keys that are converted.

func ConvertLegacyIndexOptions

func ConvertLegacyIndexOptions(indexOptions bson.M)

ConvertLegacyIndexOptions removes options that don't match a known list of index options. It is preferable to use the ignoreUnknownIndexOptions on the createIndex command to force the server to do this task. But that option was only added in 4.1.9. So for pre 3.4 indexes being added to servers 3.4 - 4.2, we must strip the options in the client. This function processes the indexes Options inside collection dump.

func ConvertLegacyIndexOptionsFromOp

func ConvertLegacyIndexOptionsFromOp(indexOptions *bson.D)

ConvertLegacyIndexOptionsFromOp removes options that don't match a known list of index options. It is preferable to use the ignoreUnknownIndexOptions on the createIndex command to force the server to do this task. But that option was only added in 4.1.9. So for pre 3.4 indexes being added to servers 3.4 - 4.2, we must strip the options in the client. This function processes the index options inside createIndexes command.

func CreateExtJSONString

func CreateExtJSONString(doc interface{}) string

CreateExtJSONString stringifies doc as Extended JSON. It does not error if it's unable to marshal the doc to JSON.

func FindIntByKey

func FindIntByKey(keyName string, document *bson.D) (int, error)

FindIntByKey returns the value of keyName in the document as an int for either int32 or int64 underlying type.

func FindStringValueByKey

func FindStringValueByKey(keyName string, document *bson.D) (string, error)

FindStringValueByKey returns the value of keyName in document as a String. Returns an error if keyName is not found in the top-level of the document, or if it is found but its value is not a string.

func FindSubdocumentByKey

func FindSubdocumentByKey(keyName string, document *bson.D) (bson.D, error)

FindSubdocumentByKey returns the value of keyName in document as a document. Returns an error if keyName is not found in the top-level of the document, or if it is found but its value is not a document.

func FindValueByKey

func FindValueByKey(keyName string, document *bson.D) (interface{}, error)

FindValueByKey returns the value of keyName in document. If keyName is not found in the top-level of the document, ErrNoSuchField is returned as the error.

func GetBSONValueAsLegacyExtJSON

func GetBSONValueAsLegacyExtJSON(x interface{}) (interface{}, error)

GetBSONValueAsLegacyExtJSON is equivalent to ConvertBSONValueToLegacyExtJSON, but does not mutate its argument.

func GetExtendedBsonD

func GetExtendedBsonD(doc bson.D) (bson.D, error)

GetExtendedBsonD iterates through the document and returns a bson.D that adds type information for each key in document.

func IsEqual

func IsEqual(left, right bson.D) (bool, error)

IsEqual marshals two documents to raw BSON and compares them.

func IsIndexKeysEqual

func IsIndexKeysEqual(indexKey1 bson.D, indexKey2 bson.D) bool

func MakeSortString

func MakeSortString(sortObj bson.D) ([]string, error)

MakeSortString takes a bson.D object and converts it to a slice of strings that can be used as the input args to mgo's .Sort(...) function. For example: {a:1, b:-1} -> ["+a", "-b"]

func MtoD

func MtoD(m bson.M) bson.D

MtoD converts a bson.M to a bson.D

func ParseLegacyExtJSONValue

func ParseLegacyExtJSONValue(jsonValue interface{}) (interface{}, error)

ParseLegacyExtJSONValue takes any value generated by the json package and returns a BSON version of that value.

func ParseSpecialKeys

func ParseSpecialKeys(special interface{}) (interface{}, error)

ParseSpecialKeys takes a JSON document and inspects it for any extended JSON type (e.g $numberLong) and replaces any such values with the corresponding BSON type. (uses legacy extJSON parser)

func RemoveKey

func RemoveKey(key string, document *bson.D) (interface{}, bool)

RemoveKey removes the given key. Returns the removed value and true if the key was found.

Types

type MarshalD

type MarshalD bson.D

MarshalD is a wrapper for bson.D that allows unmarshalling of bson.D with preserved order. Necessary for printing certain database commands.

func (MarshalD) MarshalJSON

func (md MarshalD) MarshalJSON() ([]byte, error)

MarshalJSON makes the MarshalD type usable by the encoding/json package.

Jump to

Keyboard shortcuts

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