mongo::BSONObj Class Reference

#include <bsonobj.h>

Inheritance diagram for mongo::BSONObj:
Inheritance graph
[legend]

List of all members.

Classes

class  Holder

Public Types

typedef BSONObjIterator iterator
enum  MatchType {
  Equality = 0, LT = 0x1, LTE = 0x3, GTE = 0x6,
  GT = 0x4, opIN = 0x8, NE = 0x9, opSIZE = 0x0A,
  opALL = 0x0B, NIN = 0x0C, opEXISTS = 0x0D, opMOD = 0x0E,
  opTYPE = 0x0F, opREGEX = 0x10, opOPTIONS = 0x11, opELEM_MATCH = 0x12,
  opNEAR = 0x13, opWITHIN = 0x14, opMAX_DISTANCE = 0x15
}

Public Member Functions

int addFields (BSONObj &from, set< string > &fields)
void appendSelfToBufBuilder (BufBuilder &b) const
BSONObjIterator begin ()
 BSONObj ()
 BSONObj (const Record *r)
 BSONObj (const char *msgdata, bool ifree=false)
BSONObj clientReadable () const
BSONObj copy () const
void dump () const
void elems (list< BSONElement > &) const
void elems (vector< BSONElement > &) const
BSONObj extractFields (const BSONObj &pattern, bool fillWithNull=false) const
BSONObj extractFieldsUnDotted (BSONObj pattern) const
BSONObj filterFieldsUndotted (const BSONObj &filter, bool inFilter) const
BSONElement firstElement () const
bool getBoolField (const char *name) const
BSONElement getField (const StringData &name) const
BSONElement getFieldDotted (const string &name) const
BSONElement getFieldDotted (const char *name) const
BSONElement getFieldDottedOrArray (const char *&name) const
int getFieldNames (set< string > &fields) const
void getFieldsDotted (const StringData &name, BSONElementSet &ret) const
BSONElement getFieldUsingIndexNames (const char *fieldName, const BSONObj &indexKey) const
int getIntField (const char *name) const
BSONObj getObjectField (const char *name) const
bool getObjectID (BSONElement &e) const
BSONObj getOwned () const
const char * getStringField (const char *name) const
bool hasElement (const char *name) const
bool hasField (const char *name) const
int hash () const
string hexDump () const
bool isEmpty () const
bool isOwned () const
bool isValid ()
string jsonString (JsonStringFormat format=Strict, int pretty=0) const
string md5 () const
int nFields () const
const char * objdata () const
int objsize () const
bool okForStorage () const
bool operator< (const BSONObj &other) const
bool operator<= (const BSONObj &other) const
bool operator== (const BSONObj &other) const
bool operator> (const BSONObj &other) const
bool operator>= (const BSONObj &other) const
BSONElement operator[] (int field) const
BSONElement operator[] (const string &field) const
BSONElement operator[] (const char *field) const
BSONObj replaceFieldNames (const BSONObj &obj) const
void toString (StringBuilder &s, bool isArray=false, bool full=false) const
string toString (bool isArray=false, bool full=false) const
bool valid () const
template<class T >
void vals (list< T > &) const
template<class T >
void vals (vector< T > &) const
template<class T >
void Vals (list< T > &) const
template<class T >
void Vals (vector< T > &) const
int woCompare (const BSONObj &r, const BSONObj &ordering=BSONObj(), bool considerFieldName=true) const
int woCompare (const BSONObj &r, const Ordering &o, bool considerFieldName=true) const
bool woEqual (const BSONObj &r) const
int woSortOrder (const BSONObj &r, const BSONObj &sortKey, bool useDotted=false) const
 ~BSONObj ()

Private Member Functions

void _assertInvalid () const
void init (const char *data, bool ifree)

Private Attributes

boost::shared_ptr< Holder_holder
const char * _objdata

Friends

class BSONObjIterator

Detailed Description

C++ representation of a "BSON" object -- that is, an extended JSON-style object in a binary representation.

See bsonspec.org.

Note that BSONObj's have a smart pointer capability built in -- so you can pass them around by value. The reference counts used to implement this do not use locking, so copying and destroying BSONObj's are not thread-safe operations.

BSON object format:

code <unsigned totalsize>=""> {<byte bsontype>=""><cstring fieldname>=""><Data>}* EOO

totalSize includes itself.

Data: Bool: <byte> EOO: nothing follows Undefined: nothing follows OID: an OID object NumberDouble: <double> NumberInt: <int32> String: <unsigned32 strsizewithnull>=""><cstring> Date: <8bytes> Regex: <cstring regex>=""><cstring options>=""> Object: a nested object, leading with its entire size, which terminates with EOO. Array: same as object DBRef: <strlen> <cstring ns>=""> <oid> DBRef: a database reference: basically a collection name plus an Object ID BinData: <int len>=""> <byte subtype>=""> <byte[len] data> Code: a function (not a closure): same format as String. Symbol: a language symbol (say a python symbol). same format as String. Code With Scope: <total size>=""><String><Object>

Definition at line 68 of file bsonobj.h.


Member Typedef Documentation

Definition at line 371 of file bsonobj.h.


Member Enumeration Documentation

Enumerator:
Equality 
LT 
LTE 
GTE 
GT 
opIN 
NE 
opSIZE 
opALL 
NIN 
opEXISTS 
opMOD 
opTYPE 
opREGEX 
opOPTIONS 
opELEM_MATCH 
opNEAR 
opWITHIN 
opMAX_DISTANCE 

Definition at line 322 of file bsonobj.h.


Constructor & Destructor Documentation

mongo::BSONObj::BSONObj ( const char *  msgdata,
bool  ifree = false 
) [inline, explicit]

Construct a BSONObj from data in the proper format.

Parameters:
ifree true if the BSONObj should free() the msgdata when it destructs.

Definition at line 75 of file bsonobj.h.

mongo::BSONObj::BSONObj ( const Record r  )  [inline, explicit]

Definition at line 492 of file pdfile.h.

mongo::BSONObj::BSONObj (  )  [inline]

Construct an empty BSONObj -- that is, {}.

Definition at line 566 of file bson-inl.h.

mongo::BSONObj::~BSONObj (  )  [inline]

Definition at line 84 of file bsonobj.h.


Member Function Documentation

NOINLINE_DECL void mongo::BSONObj::_assertInvalid (  )  const [inline, private]

Definition at line 50 of file bson-inl.h.

int mongo::BSONObj::addFields ( BSONObj from,
set< string > &  fields 
)

note: addFields always adds _id even if not specified

void mongo::BSONObj::appendSelfToBufBuilder ( BufBuilder b  )  const [inline]

Definition at line 381 of file bsonobj.h.

BSONObjIterator mongo::BSONObj::begin (  )  [inline]

use something like this: for( BSONObj::iterator i = myObj.begin(); i.more(); ) { BSONElement e = i.next(); ... }

Definition at line 26 of file bson-inl.h.

BSONObj mongo::BSONObj::clientReadable (  )  const
NOINLINE_DECL BSONObj mongo::BSONObj::copy (  )  const [inline]
Returns:
a new full (and owned) copy of the object.

Definition at line 65 of file bson-inl.h.

void mongo::BSONObj::dump (  )  const
void mongo::BSONObj::elems ( list< BSONElement > &  v  )  const [inline]

add all elements of the object to the specified list

Definition at line 588 of file bson-inl.h.

void mongo::BSONObj::elems ( vector< BSONElement > &  v  )  const [inline]

add all elements of the object to the specified vector

Definition at line 582 of file bson-inl.h.

BSONObj mongo::BSONObj::extractFields ( const BSONObj pattern,
bool  fillWithNull = false 
) const

extract items from object which match a pattern object. e.g., if pattern is { x : 1, y : 1 }, builds an object with x and y elements of this object, if they are present. returns elements with original field names

BSONObj mongo::BSONObj::extractFieldsUnDotted ( BSONObj  pattern  )  const

sets element field names to empty string If a field in pattern is missing, it is omitted from the returned object.

BSONObj mongo::BSONObj::filterFieldsUndotted ( const BSONObj filter,
bool  inFilter 
) const
BSONElement mongo::BSONObj::firstElement (  )  const [inline]
Returns:
first field of the object

Definition at line 281 of file bsonobj.h.

bool mongo::BSONObj::getBoolField ( const char *  name  )  const
Returns:
false if not present
BSONElement mongo::BSONObj::getField ( const StringData name  )  const [inline]

Get the field of the specified name. eoo() is true on the returned element if not found.

Definition at line 102 of file bson-inl.h.

BSONElement mongo::BSONObj::getFieldDotted ( const string &  name  )  const [inline]

return has eoo() true if no match supports "." notation to reach into embedded objects

Definition at line 151 of file bsonobj.h.

BSONElement mongo::BSONObj::getFieldDotted ( const char *  name  )  const [inline]

return has eoo() true if no match supports "." notation to reach into embedded objects

Definition at line 534 of file bson-inl.h.

BSONElement mongo::BSONObj::getFieldDottedOrArray ( const char *&  name  )  const

Like getFieldDotted(), but returns first array encountered while traversing the dotted fields of name. The name variable is updated to represent field names with respect to the returned element.

int mongo::BSONObj::getFieldNames ( set< string > &  fields  )  const

adds the field names to the fields set. does NOT clear it (appends).

void mongo::BSONObj::getFieldsDotted ( const StringData name,
BSONElementSet ret 
) const

Like getFieldDotted(), but expands multikey arrays and returns all matching objects

BSONElement mongo::BSONObj::getFieldUsingIndexNames ( const char *  fieldName,
const BSONObj indexKey 
) const
int mongo::BSONObj::getIntField ( const char *  name  )  const
Returns:
INT_MIN if not present - does some type conversions
BSONObj mongo::BSONObj::getObjectField ( const char *  name  )  const [inline]
Returns:
subobject of the given name

Definition at line 548 of file bson-inl.h.

bool mongo::BSONObj::getObjectID ( BSONElement e  )  const [inline]

Get the _id field from the object. For good performance drivers should assure that _id is the first element of the object; however, correct operation is assured regardless.

Returns:
true if found

Definition at line 148 of file bson-inl.h.

BSONObj mongo::BSONObj::getOwned (  )  const [inline]

Definition at line 71 of file bson-inl.h.

const char* mongo::BSONObj::getStringField ( const char *  name  )  const
Returns:
"" if DNE or wrong type
bool mongo::BSONObj::hasElement ( const char *  name  )  const [inline]
Returns:
true if field exists in the object

Definition at line 90 of file bson-inl.h.

bool mongo::BSONObj::hasField ( const char *  name  )  const [inline]
Returns:
true if field exists

Definition at line 187 of file bsonobj.h.

int mongo::BSONObj::hash (  )  const [inline]
Returns:
A hash code for the object

Definition at line 294 of file bsonobj.h.

string mongo::BSONObj::hexDump (  )  const

Alternative output format

void mongo::BSONObj::init ( const char *  data,
bool  ifree 
) [inline, private]

Definition at line 404 of file bsonobj.h.

bool mongo::BSONObj::isEmpty (  )  const [inline]
Returns:
true if object is empty -- i.e., {}

Definition at line 235 of file bsonobj.h.

bool mongo::BSONObj::isOwned (  )  const [inline]

A BSONObj can use a buffer it "owns" or one it does not.

OWNED CASE If the BSONObj owns the buffer, the buffer can be shared among several BSONObj's (by assignment). In this case the buffer is basically implemented as a shared_ptr. Since BSONObj's are typically immutable, this works well.

UNOWNED CASE A BSONObj can also point to BSON data in some other data structure it does not "own" or free later. For example, in a memory mapped file. In this case, it is important the original data stays in scope for as long as the BSONObj is in use. If you think the original data may go out of scope, call BSONObj::getOwned() to promote your BSONObj to having its own copy.

On a BSONObj assignment, if the source is unowned, both the source and dest will have unowned pointers to the original buffer after the assignment.

If you are not sure about ownership but need the buffer to last as long as the BSONObj, call getOwned(). getOwned() is a no-op if the buffer is already owned. If not already owned, a malloc and memcpy will result.

Most ways to create BSONObj's create 'owned' variants. Unowned versions can be created with: (1) specifying true for the ifree parameter in the constructor (2) calling BSONObjBuilder::done(). Use BSONObjBuilder::obj() to get an owned copy (3) retrieving a subobject retrieves an unowned pointer into the parent BSON object

Returns:
true if this is in owned mode

Definition at line 114 of file bsonobj.h.

bool mongo::BSONObj::isValid (  )  [inline]

performs a cursory check on the object's size only.

Definition at line 143 of file bson-inl.h.

string mongo::BSONObj::jsonString ( JsonStringFormat  format = Strict,
int  pretty = 0 
) const

Properly formatted JSON string.

Parameters:
pretty if true we try to add some lf's and indentation
string mongo::BSONObj::md5 (  )  const
Returns:
an md5 value for this object.
int mongo::BSONObj::nFields (  )  const [inline]

returns # of top level fields in the object note: iterates to count the fields

Definition at line 554 of file bson-inl.h.

const char* mongo::BSONObj::objdata (  )  const [inline]
Returns:
the raw data of the object

Definition at line 220 of file bsonobj.h.

int mongo::BSONObj::objsize (  )  const [inline]
Returns:
total size of the BSON object in bytes

Definition at line 224 of file bsonobj.h.

bool mongo::BSONObj::okForStorage (  )  const
Returns:
if the user is a valid user doc criter: isValid() no . or $ field names
bool mongo::BSONObj::operator< ( const BSONObj other  )  const [inline]

Definition at line 259 of file bsonobj.h.

bool mongo::BSONObj::operator<= ( const BSONObj other  )  const [inline]

Definition at line 260 of file bsonobj.h.

bool mongo::BSONObj::operator== ( const BSONObj other  )  const [inline]

Definition at line 318 of file bsonobj.h.

bool mongo::BSONObj::operator> ( const BSONObj other  )  const [inline]

Definition at line 261 of file bsonobj.h.

bool mongo::BSONObj::operator>= ( const BSONObj other  )  const [inline]

Definition at line 262 of file bsonobj.h.

BSONElement mongo::BSONObj::operator[] ( int  field  )  const [inline]

Definition at line 179 of file bsonobj.h.

BSONElement mongo::BSONObj::operator[] ( const string &  field  )  const [inline]

Definition at line 175 of file bsonobj.h.

BSONElement mongo::BSONObj::operator[] ( const char *  field  )  const [inline]

Get the field of the specified name. eoo() is true on the returned element if not found.

Definition at line 171 of file bsonobj.h.

BSONObj mongo::BSONObj::replaceFieldNames ( const BSONObj obj  )  const

Return new object with the field names replaced by those in the passed object.

void mongo::BSONObj::toString ( StringBuilder s,
bool  isArray = false,
bool  full = false 
) const [inline]

Definition at line 272 of file bson-inl.h.

string mongo::BSONObj::toString ( bool  isArray = false,
bool  full = false 
) const [inline]

Readable representation of a BSON object in an extended JSON-style notation. This is an abbreviated representation which might be used for logging.

Definition at line 266 of file bson-inl.h.

bool mongo::BSONObj::valid (  )  const

true unless corrupt

template<class T >
void mongo::BSONObj::vals ( list< T > &  v  )  const [inline]

add all values of the object to the specified list. If type mismatches, skip.

Definition at line 626 of file bson-inl.h.

template<class T >
void mongo::BSONObj::vals ( vector< T > &  v  )  const [inline]

add all values of the object to the specified vector. If type mismatches, skip.

Definition at line 614 of file bson-inl.h.

template<class T >
void mongo::BSONObj::Vals ( list< T > &  v  )  const [inline]

add all values of the object to the specified list. If type mismatches, exception.

Definition at line 604 of file bson-inl.h.

template<class T >
void mongo::BSONObj::Vals ( vector< T > &  v  )  const [inline]

add all values of the object to the specified vector. If type mismatches, exception. this is most useful when the BSONObj is an array, but can be used with non-arrays too in theory.

example: bo sub = y["subobj"].Obj(); vector<int> myints; sub.Vals(myints);

Definition at line 595 of file bson-inl.h.

int mongo::BSONObj::woCompare ( const BSONObj r,
const BSONObj ordering = BSONObj(),
bool  considerFieldName = true 
) const

wo='well ordered'. fields must be in same order in each object. Ordering is with respect to the signs of the elements and allows ascending / descending key mixing.

Returns:
<0 if l<r. 0 if l==r. >0 if l>r
int mongo::BSONObj::woCompare ( const BSONObj r,
const Ordering o,
bool  considerFieldName = true 
) const

wo='well ordered'. fields must be in same order in each object. Ordering is with respect to the signs of the elements and allows ascending / descending key mixing.

Returns:
<0 if l<r. 0 if l==r. >0 if l>r
bool mongo::BSONObj::woEqual ( const BSONObj r  )  const [inline]

This is "shallow equality" -- ints and doubles won't match. for a deep equality test use woCompare (which is slower).

Definition at line 272 of file bsonobj.h.

int mongo::BSONObj::woSortOrder ( const BSONObj r,
const BSONObj sortKey,
bool  useDotted = false 
) const
Parameters:
useDotted whether to treat sort key fields as possibly dotted and expand into them

Friends And Related Function Documentation

friend class BSONObjIterator [friend]

Definition at line 370 of file bsonobj.h.


Member Data Documentation

boost::shared_ptr< Holder > mongo::BSONObj::_holder [private]

Definition at line 401 of file bsonobj.h.

const char* mongo::BSONObj::_objdata [private]

Definition at line 400 of file bsonobj.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


mongodb
Author(s): Nate Koenig
autogenerated on Fri Jan 11 12:15:53 2013