MongoDB C++ Driver legacy-1.0.0
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
mongo::BSONElement Class Reference

BSONElement represents an "element" in a BSONObj. More...

#include <bsonelement.h>

Classes

struct  FieldNameSizeTag
 
struct  Hasher
 Functor compatible with std::hash for std::unordered_{map,set} Warning: The hash function is subject to change. More...
 

Public Member Functions

std::string String () const
 These functions, which start with a capital letter, throw a MsgAssertionException if the element is not of the required type.
 
BSONObj Obj () const
 
void Val (Date_t &v) const
 populate v with the value of the element.
 
bool ok () const
 Use ok() to check if a value is assigned: if( myObj["foo"].ok() ) ...
 
BSONType type () const
 Returns the type of the element.
 
BSONElement operator[] (const std::string &field) const
 retrieve a field within this element throws exception if *this is not an embedded object
 
int canonicalType () const
 See canonicalizeBSONType in bsontypes.h.
 
bool eoo () const
 Indicates if it is the end-of-object element, which is present at the end of every BSON object.
 
int size (int maxLen) const
 Size of the element.
 
BSONObj wrap () const
 Wrap this element up as a singleton object.
 
BSONObj wrap (const StringData &newName) const
 Wrap this element up as a singleton object with a new name.
 
const char * fieldName () const
 field name of the element.
 
int fieldNameSize () const
 NOTE: size includes the NULL terminator.
 
const char * value () const
 raw data of the element's value (so be careful).
 
int valuesize () const
 size in bytes of the element's value (when applicable).
 
bool boolean () const
 
Date_t date () const
 Retrieve a java style date value from the element.
 
bool trueValue () const
 Convert the value to boolean, regardless of its type, in a javascript-like fashion (i.e., treats zero and null and eoo as false).
 
bool isSimpleType () const
 True if number, string, bool, date, OID.
 
bool isNumber () const
 True if element is of a numeric type.
 
double _numberDouble () const
 Return double value for this field.
 
int _numberInt () const
 Return int value for this field.
 
long long _numberLong () const
 Return long long value for this field.
 
int numberInt () const
 Retrieve int value for the element safely.
 
long long numberLong () const
 Retrieve long value for the element safely.
 
long long safeNumberLong () const
 Like numberLong() but with well-defined behavior for doubles that are NaNs, or too large/small to be represented as long longs.
 
double numberDouble () const
 Retrieve the numeric value of the element.
 
double number () const
 Retrieve the numeric value of the element.
 
mongo::OID __oid () const
 Retrieve the object ID stored in the object.
 
bool isNull () const
 True if element is null.
 
int valuestrsize () const
 Size (length) of a string element.
 
const char * valuestr () const
 Get a string's value.
 
const char * valuestrsafe () const
 Get the string value of the element.
 
std::string str () const
 Get the string value of the element.
 
const StringData valueStringData () const
 Returns a StringData pointing into this element's data.
 
const char * codeWScopeCode () const
 Get javascript code of a CodeWScope data element.
 
int codeWScopeCodeLen () const
 Get length of the code part of the CodeWScope object This INCLUDES the null char at the end.
 
const char * codeWScopeScopeDataUnsafe () const
 Get the scope SavedContext of a CodeWScope data element.
 
BSONObj embeddedObject () const
 Get the embedded object this element holds.
 
const char * binData (int &len) const
 Get raw binary data.
 
const char * binDataClean (int &len) const
 Get binary data.
 
const char * regex () const
 Retrieve the regex string for a Regex element.
 
const char * regexFlags () const
 Retrieve the regex flags (options) for a Regex element.
 
bool valuesEqual (const BSONElement &r) const
 like operator== but doesn't check the fieldname, just the value.
 
bool operator== (const BSONElement &r) const
 Returns true if elements are equal.
 
bool operator!= (const BSONElement &r) const
 Returns true if elements are unequal.
 
int woCompare (const BSONElement &e, bool considerFieldName=true) const
 Well ordered comparison.
 
int getGtLtOp (int def=0) const
 0 == Equality, just not defined yet
 
 BSONElement ()
 Constructs an empty element.
 
bool mayEncapsulate () const
 True if this element may contain subobjects.
 
bool isABSONObj () const
 True if this element can be a BSONObj.
 
bool operator< (const BSONElement &other) const
 this does not use fieldName in the comparison, just the value
 
 BSONElement (const char *d, int fieldNameSize, FieldNameSizeTag)
 Construct a BSONElement where you already know the length of the name.
 

Detailed Description

BSONElement represents an "element" in a BSONObj.

So for the object { a : 3, b : "abc" }, 'a : 3' is the first element (key+value).

The BSONElement object points into the BSONObj's data. Thus the BSONObj must stay in scope for the life of the BSONElement.

internals: <type><fieldName >

-----— size() ---------— -fieldNameSize- value() type()

Constructor & Destructor Documentation

◆ BSONElement()

mongo::BSONElement::BSONElement ( const char *  d,
int  fieldNameSize,
FieldNameSizeTag   
)
inline

Construct a BSONElement where you already know the length of the name.

The value passed here includes the null terminator. The data pointed to by 'd' must not represent an EOO. You may pass -1 to indicate that you don't actually know the size.

Member Function Documentation

◆ __oid()

mongo::OID mongo::BSONElement::__oid ( ) const
inline

Retrieve the object ID stored in the object.

You must ensure the element is of type jstOID first.

◆ _numberDouble()

double mongo::BSONElement::_numberDouble ( ) const
inline

Return double value for this field.

MUST be NumberDouble type.

◆ _numberInt()

int mongo::BSONElement::_numberInt ( ) const
inline

Return int value for this field.

MUST be NumberInt type.

◆ _numberLong()

long long mongo::BSONElement::_numberLong ( ) const
inline

Return long long value for this field.

MUST be NumberLong type.

◆ binData()

const char * mongo::BSONElement::binData ( int &  len) const
inline

Get raw binary data.

Element must be of type BinData. Doesn't handle type 2 specially

◆ binDataClean()

const char * mongo::BSONElement::binDataClean ( int &  len) const
inline

Get binary data.

Element must be of type BinData. Handles type 2

◆ boolean()

bool mongo::BSONElement::boolean ( ) const
inline
Returns
value of a boolean element. You must assure element is a boolean before calling.

◆ codeWScopeScopeDataUnsafe()

const char * mongo::BSONElement::codeWScopeScopeDataUnsafe ( ) const
inline

Get the scope SavedContext of a CodeWScope data element.

This function is DEPRECATED, since it can error if there are null chars in the codeWScopeCode. However, some existing indexes may be based on an incorrect ordering derived from this function, so it may still need to be used in certain cases.

◆ date()

Date_t mongo::BSONElement::date ( ) const
inline

Retrieve a java style date value from the element.

Ensure element is of type Date before calling.

See also
Bool(), trueValue()

◆ fieldName()

const char * mongo::BSONElement::fieldName ( ) const
inline

field name of the element.

e.g., for name : "Joe" "name" is the fieldname

◆ isNumber()

bool mongo::BSONElement::isNumber ( ) const
inline

True if element is of a numeric type.

Returns
true if element is of a numeric type.

◆ number()

double mongo::BSONElement::number ( ) const
inline

Retrieve the numeric value of the element.

If not of a numeric type, returns 0. Note: casts to double, data loss may occur with large (>52 bit) NumberLong values.

◆ numberDouble()

double mongo::BSONElement::numberDouble ( ) const
inline

Retrieve the numeric value of the element.

If not of a numeric type, returns 0. Note: casts to double, data loss may occur with large (>52 bit) NumberLong values.

◆ numberInt()

int mongo::BSONElement::numberInt ( ) const
inline

Retrieve int value for the element safely.

Zero returned if not a number.

Zero returned if not a number. Converted to int if another numeric type.

◆ numberLong()

long long mongo::BSONElement::numberLong ( ) const
inline

Retrieve long value for the element safely.

Zero returned if not a number. Behavior is not defined for double values that are NaNs, or too large/small to be represented by long longs

Zero returned if not a number.

◆ Obj()

BSONObj mongo::BSONElement::Obj ( ) const
Returns
the embedded object associated with this field. Note the returned object is a reference to within the parent bson object. If that object is out of scope, this pointer will no longer be valid. Call getOwned() on the returned BSONObj if you need your own copy. throws UserException if the element is not of type object.

◆ safeNumberLong()

long long mongo::BSONElement::safeNumberLong ( ) const
inline

Like numberLong() but with well-defined behavior for doubles that are NaNs, or too large/small to be represented as long longs.

NaNs -> 0 very large doubles -> LLONG_MAX very small doubles -> LLONG_MIN

◆ size()

int mongo::BSONElement::size ( int  maxLen) const

Size of the element.

Parameters
maxLenIf maxLen is specified, don't scan more than maxLen bytes to calculate size.

◆ str()

std::string mongo::BSONElement::str ( ) const
inline

Get the string value of the element.

If not a string returns "".

◆ String()

std::string mongo::BSONElement::String ( ) const
inline

These functions, which start with a capital letter, throw a MsgAssertionException if the element is not of the required type.

Example:

std::string foo = obj["foo"].String(); // std::exception if not a std::string type or DNE

◆ Val()

void mongo::BSONElement::Val ( Date_t v) const
inline

populate v with the value of the element.

If type does not match, throw exception. useful in templates – see also BSONObj::Vals().

◆ valuestr()

const char * mongo::BSONElement::valuestr ( ) const
inline

Get a string's value.

Also gives you start of the real data for an embedded object. You must assure data is of an appropriate type first – see also valuestrsafe().

◆ valueStringData()

const StringData mongo::BSONElement::valueStringData ( ) const
inline

Returns a StringData pointing into this element's data.

Does not validate that the element is actually of type String.

◆ valuestrsafe()

const char * mongo::BSONElement::valuestrsafe ( ) const
inline

Get the string value of the element.

If not a string returns "".

◆ valuestrsize()

int mongo::BSONElement::valuestrsize ( ) const
inline

Size (length) of a string element.

You must assure of type String first.

Returns
string size including terminating null

◆ woCompare()

int mongo::BSONElement::woCompare ( const BSONElement e,
bool  considerFieldName = true 
) const

Well ordered comparison.

Returns
<0: l<r. 0:l==r. >0:l>r order by type, field name, and field value. If considerFieldName is true, pay attention to the field name.

The documentation for this class was generated from the following file: