SequoiaDB
 All Classes Namespaces Files Functions Macros Pages
Public Member Functions | Friends | List of all members
bson::BSONElement Class Reference

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

#include <bsonelement.h>

Public Member Functions

string String () const
 These functions, which start with a capital letter, throw a UserException if the element is not of the required type.
 
Date_t Date () const
 
double Number () const
 
double Double () const
 
long long Long () const
 
bsonDecimal Decimal () const
 
int Int () const
 
bool Bool () const
 
vector< BSONElementArray () const
 
bson::OID OID () const
 
void Null () const
 
void OK () const
 
BSONObj Obj () const
 
void Val (Date_t &v) const
 populate v with the value of the element.
 
void Val (long long &v) const
 
void Val (bool &v) const
 
void Val (BSONObj &v) const
 
void Val (bson::OID &v) const
 
void Val (int &v) const
 
void Val (double &v) const
 
void Val (string &v) const
 
void Val (bsonDecimal &v) const
 
bool ok () const
 Use ok() to check if a value is assigned: if( myObj["foo"].ok() ) ...
 
string toString (bool includeFieldName=true, bool full=false) const
 
void toString (StringBuilder &s, bool includeFieldName=true, bool full=false) const
 
string jsonString (JsonStringFormat format, bool includeFieldNames=true, int pretty=0) const
 
 operator string () const
 
BSONType type () const
 Returns the type of the element.
 
BSONElement operator[] (const string &field) const
 retrieve a field within this element throws exception if *this is not an embedded object
 
int canonicalType () const
 returns the type of the element fixed for the main type the main purpose is numbers.
 
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.
 
int size () const
 
BSONObj wrap () const
 Wrap this element up as a singleton object.
 
BSONObj wrap (const char *newName) const
 Wrap this element up as a singleton object with a new name.
 
const char * fieldName () const
 field name of the element.
 
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 isBoolean () const
 
bool boolean () const
 
bool booleanSafe () 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 double value for this field.
 
long long _numberLong () const
 Return double 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.
 
bsonDecimal numberDecimal () const
 Retrieve long value for the element safely.
 
double numberDouble () const
 Retrieve the numeric value of the element.
 
double number () const
 Retrieve the numeric value of the element.
 
const bson::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.
 
int objsize () const
 
const char * valuestr () const
 Get a string's value.
 
const char * valuestrsafe () const
 Get the string value of the element.
 
string str () const
 Get the string value of the element.
 
const char * codeWScopeCode () const
 Get javascript code of a CodeWScope data element.
 
const char * codeWScopeScopeData () const
 Get the scope SavedContext of a CodeWScope data element.
 
BSONObj embeddedObject () const
 Get the embedded object this element holds.
 
BSONObj embeddedObjectUserCheck () const
 
BSONObj codeWScopeObject () const
 
const char * binData (int &len) const
 Get raw binary data.
 
const char * binDataClean (int &len) const
 Get binary data.
 
BinDataType binDataType () const
 
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.
 
string code () const
 Retrieve the code string for a Code 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.
 
int woCompare (const BSONElement &e, bool considerFieldName=true) const
 Well ordered comparison.
 
const char * rawdata () const
 
int getGtLtOp (int def=0) const
 0 == Equality, just not defined yet
 
 BSONElement ()
 Constructs an empty element.
 
void validate () const
 Check that data is internally consistent.
 
bool mayEncapsulate () const
 True if this element may contain subobjects.
 
bool isABSONObj () const
 True if this element can be a BSONObj.
 
Date_t timestampTime () const
 
unsigned int timestampInc () const
 
const char * dbrefNS () const
 
const bson::OID & dbrefOID () const
 
bool operator< (const BSONElement &other) const
 this does not use fieldName in the comparison, just the value
 
 BSONElement (const char *d, int maxLen)
 
 BSONElement (const char *d)
 
string _asCode () const
 
OpTime _opTime () const
 

Friends

class BSONObjIterator
 
class BSONObj
 

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()

Member Function Documentation

const bson::OID& bson::BSONElement::__oid ( ) const
inline

Retrieve the object ID stored in the object.

You must ensure the element is of type jstOID first.

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

Return double value for this field.

MUST be NumberDouble type.

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

Return double value for this field.

MUST be NumberInt type.

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

Return double value for this field.

MUST be NumberLong type.

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

Get raw binary data.

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

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

Get binary data.

Element must be of type BinData. Handles type 2

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

returns the type of the element fixed for the main type the main purpose is numbers.

any numeric type will return NumberDouble

Note: if the order changes, indexes have to be re-built or than can be corruption

const char* bson::BSONElement::codeWScopeCode ( ) const
inline

Get javascript code of a CodeWScope data element.

const char* bson::BSONElement::codeWScopeScopeData ( ) const
inline

Get the scope SavedContext of a CodeWScope data element.

Date_t bson::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()
BSONObj bson::BSONElement::embeddedObject ( ) const

Get the embedded object this element holds.

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

field name of the element.

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

bool bson::BSONElement::isNull ( ) const
inline

True if element is null.

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

True if element is of a numeric type.

Returns
true if element is of a numeric type.
bool bson::BSONElement::mayEncapsulate ( ) const
inline

True if this element may contain subobjects.

double bson::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.

bsonDecimal bson::BSONElement::numberDecimal ( ) const
inline

Retrieve long value for the element safely.

Zero returned if not a number.

double bson::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.

int bson::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.

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

Retrieve long value for the element safely.

Zero returned if not a number.

BSONObj bson::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.
bool bson::BSONElement::operator== ( const BSONElement r) const
inline

Returns true if elements are equal.

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

Size of the element.

Parameters
maxLenIf maxLen is specified, don't scan more than maxLen bytes to calculate size.
string bson::BSONElement::str ( ) const
inline

Get the string value of the element.

If not a string returns "".

string bson::BSONElement::String ( ) const
inline

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

Example:

       string foo = obj["foo"].String();

exception if not a string type or DNE

void bson::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().
void bson::BSONElement::validate ( ) const

Check that data is internally consistent.

const char* bson::BSONElement::value ( ) const
inline

raw data of the element's value (so be careful).

int bson::BSONElement::valuesize ( ) const
inline

size in bytes of the element's value (when applicable).

const char* bson::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().

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

Get the string value of the element.

If not a string returns "".

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

Size (length) of a string element.

You must assure of type String first.

Returns
string size including terminating null
int bson::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.
BSONObj bson::BSONElement::wrap ( ) const

Wrap this element up as a singleton object.

BSONObj bson::BSONElement::wrap ( const char *  newName) const

Wrap this element up as a singleton object with a new name.


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