MongoDB  2.1.1-pre-
Classes | Public Member Functions | Static Public Member Functions
mongo::BSONObjBuilder Class Reference

Utility for creating a BSONObj. More...

#include <bsonobjbuilder.h>

List of all members.

Classes

struct  ForceExplicitString

Public Member Functions

 BSONObjBuilder (int initsize=512)
 BSONObjBuilder (BufBuilder &baseBuilder)
 BSONObjBuilder (const BSONSizeTracker &tracker)
BSONObjBuilderappendElements (BSONObj x)
 add all the fields from the object specified to this object
BSONObjBuilderappendElementsUnique (BSONObj x)
 add all the fields from the object specified to this object if they don't exist already
BSONObjBuilderappend (const BSONElement &e)
 append element to the object we are building
BSONObjBuilderappendAs (const BSONElement &e, const StringData &fieldName)
 append an element but with a new name
BSONObjBuilderappend (const StringData &fieldName, BSONObj subObj)
 add a subobject as a member
BSONObjBuilderappendObject (const StringData &fieldName, const char *objdata, int size=0)
 add a subobject as a member
BufBuildersubobjStart (const StringData &fieldName)
 add header for a new subobject and return bufbuilder for writing to the subobject's body
BSONObjBuilderappendArray (const StringData &fieldName, const BSONObj &subObj)
 add a subobject as a member with type Array.
BSONObjBuilderappend (const StringData &fieldName, BSONArray arr)
BufBuildersubarrayStart (const StringData &fieldName)
 add header for a new subarray and return bufbuilder for writing to the subarray's body
BSONObjBuilderappendBool (const StringData &fieldName, int val)
 Append a boolean element.
BSONObjBuilderappend (const StringData &fieldName, bool val)
 Append a boolean element.
BSONObjBuilderappend (const StringData &fieldName, int n)
 Append a 32 bit integer element.
BSONObjBuilderappend (const StringData &fieldName, unsigned n)
 Append a 32 bit unsigned element - cast to a signed int.
BSONObjBuilderappend (const StringData &fieldName, long long n)
 Append a NumberLong.
BSONObjBuilderappendIntOrLL (const StringData &fieldName, long long n)
 appends a number.
BSONObjBuilderappendNumber (const StringData &fieldName, int n)
 appendNumber is a series of method for appending the smallest sensible type mostly for JS
BSONObjBuilderappendNumber (const StringData &fieldName, double d)
BSONObjBuilderappendNumber (const StringData &fieldName, size_t n)
BSONObjBuilderappendNumber (const StringData &fieldName, long long l)
BSONObjBuilderappend (const StringData &fieldName, double n)
 Append a double element.
bool appendAsNumber (const StringData &fieldName, const string &data)
 tries to append the data as a number
BSONObjBuilderappendOID (const StringData &fieldName, OID *oid=0, bool generateIfBlank=false)
 Append a BSON Object ID (OID type).
BSONObjBuilderappend (const StringData &fieldName, OID oid)
 Append a BSON Object ID.
BSONObjBuildergenOID ()
 Generate and assign an object id for the _id field.
BSONObjBuilderappendTimeT (const StringData &fieldName, time_t dt)
 Append a time_t date.
BSONObjBuilderappendDate (const StringData &fieldName, Date_t dt)
 Append a date.
BSONObjBuilderappend (const StringData &fieldName, Date_t dt)
BSONObjBuilderappendRegex (const StringData &fieldName, const StringData &regex, const StringData &options="")
 Append a regular expression value.
BSONObjBuilderappendCode (const StringData &fieldName, const StringData &code)
BSONObjBuilderappend (const StringData &fieldName, const char *str, int sz)
 Append a string element.
BSONObjBuilderappend (const StringData &fieldName, const char *str)
 Append a string element.
BSONObjBuilderappend (const StringData &fieldName, const string &str)
 Append a string element.
BSONObjBuilderappendSymbol (const StringData &fieldName, const StringData &symbol)
BSONObjBuilderappendNull (const StringData &fieldName)
 Append a Null element to the object.
BSONObjBuilderappendMinKey (const StringData &fieldName)
BSONObjBuilderappendMaxKey (const StringData &fieldName)
BSONObjBuilderappendTimestamp (const StringData &fieldName)
BSONObjBuilderappendTimestamp (const StringData &fieldName, unsigned long long val)
BSONObjBuilderappendTimestamp (const StringData &fieldName, unsigned long long time, unsigned int inc)
 Timestamps are a special BSON datatype that is used internally for replication.
BSONObjBuilderappendDBRef (const StringData &fieldName, const StringData &ns, const OID &oid)
BSONObjBuilderappendBinData (const StringData &fieldName, int len, BinDataType type, const void *data)
 Append a binary data element.
BSONObjBuilderappendBinDataArrayDeprecated (const char *fieldName, const void *data, int len)
 Subtype 2 is deprecated.
BSONObjBuilderappendCodeWScope (const StringData &fieldName, const StringData &code, const BSONObj &scope)
 Append to the BSON object a field of type CodeWScope.
void appendUndefined (const StringData &fieldName)
void appendWhere (const StringData &code, const BSONObj &scope)
void appendMinForType (const StringData &fieldName, int type)
 these are the min/max when comparing, not strict min/max elements for a given type
void appendMaxForType (const StringData &fieldName, int type)
template<class T >
BSONObjBuilderappend (const StringData &fieldName, const vector< T > &vals)
 Append an array of values.
template<class T >
BSONObjBuilderappend (const StringData &fieldName, const list< T > &vals)
template<class T >
BSONObjBuilderappend (const StringData &fieldName, const set< T > &vals)
 Append a set of values.
BSONObj obj ()
 destructive The returned BSONObj will free the buffer when it is finished.
BSONObj done ()
 Fetch the object we have built.
void doneFast ()
BSONObj asTempObj ()
 Peek at what is in the builder, but leave the builder ready for more appends.
char * decouple (int &l)
void decouple ()
void appendKeys (const BSONObj &keyPattern, const BSONObj &values)
BSONObjBuilderValueStreamoperator<< (const char *name)
 Stream oriented way to add field names and values.
BSONObjBuilderoperator<< (GENOIDLabeler)
 Stream oriented way to add field names and values.
BSONObjBuilderValueStreamoperator<< (const ForceExplicitString &name)
 Stream oriented way to add field names and values.
Labeler operator<< (const Labeler::Label &l)
template<typename T >
BSONObjBuilderValueStreamoperator<< (const BSONField< T > &f)
template<typename T >
BSONObjBuilderoperator<< (const BSONFieldValue< T > &v)
BSONObjBuilderoperator<< (const BSONElement &e)
bool owned () const
BSONObjIterator iterator () const
bool hasField (const StringData &name) const
int len () const
BufBuilderbb ()

Static Public Member Functions

static string numStr (int i)

Detailed Description

Utility for creating a BSONObj.

See also the BSON() and BSON_ARRAY() macros.


Constructor & Destructor Documentation

mongo::BSONObjBuilder::BSONObjBuilder ( int  initsize = 512) [inline]
Parameters:
initsizethis is just a hint as to the final size of the object
Parameters:
baseBuilderconstruct a BSONObjBuilder using an existing BufBuilder This is for more efficient adding of subobjects/arrays. See docs for subobjStart for example.

Member Function Documentation

BSONObjBuilder& mongo::BSONObjBuilder::append ( const StringData fieldName,
unsigned  n 
) [inline]

Append a 32 bit unsigned element - cast to a signed int.

BSONObjBuilder& mongo::BSONObjBuilder::append ( const StringData fieldName,
OID  oid 
) [inline]

Append a BSON Object ID.

Parameters:
fieldNameField name, e.g., "_id".
Returns:
the builder object
BSONObjBuilder& mongo::BSONObjBuilder::append ( const StringData fieldName,
const char *  str,
int  sz 
) [inline]

Append a string element.

Parameters:
szsize includes terminating null character
template<class T >
BSONObjBuilder & mongo::BSONObjBuilder::append ( const StringData fieldName,
const vector< T > &  vals 
) [inline]

Append an array of values.

template<class T >
BSONObjBuilder & mongo::BSONObjBuilder::append ( const StringData fieldName,
const set< T > &  vals 
) [inline]

Append a set of values.

BSONObjBuilder& mongo::BSONObjBuilder::appendArray ( const StringData fieldName,
const BSONObj subObj 
) [inline]

add a subobject as a member with type Array.

Thus arr object should have "0", "1", ... style fields in it.

bool mongo::BSONObjBuilder::appendAsNumber ( const StringData fieldName,
const string &  data 
)

tries to append the data as a number

Returns:
true if the data was able to be converted to a number
BSONObjBuilder& mongo::BSONObjBuilder::appendBinData ( const StringData fieldName,
int  len,
BinDataType  type,
const void *  data 
) [inline]

Append a binary data element.

Parameters:
fieldNamename of the field
lenlength of the binary data in bytes
subtypesubtype information for the data.
See also:
enum BinDataType in bsontypes.h. Use BinDataGeneral if you don't care about the type.
Parameters:
datathe byte array
BSONObjBuilder& mongo::BSONObjBuilder::appendBinDataArrayDeprecated ( const char *  fieldName,
const void *  data,
int  len 
) [inline]

Subtype 2 is deprecated.

Append a BSON bindata bytearray element.

Parameters:
dataa byte array
lenthe length of data
BSONObjBuilder& mongo::BSONObjBuilder::appendCodeWScope ( const StringData fieldName,
const StringData code,
const BSONObj scope 
) [inline]

Append to the BSON object a field of type CodeWScope.

This is a javascript code fragment accompanied by some scope that goes with it.

BSONObjBuilder& mongo::BSONObjBuilder::appendDate ( const StringData fieldName,
Date_t  dt 
) [inline]

Append a date.

Parameters:
dta Java-style 64 bit date value, that is the number of milliseconds since January 1, 1970, 00:00:00 GMT
BSONObjBuilder& mongo::BSONObjBuilder::appendIntOrLL ( const StringData fieldName,
long long  n 
) [inline]

appends a number.

if n < max(int)/2 then uses int, otherwise long long

BSONObjBuilder& mongo::BSONObjBuilder::appendOID ( const StringData fieldName,
OID oid = 0,
bool  generateIfBlank = false 
) [inline]

Append a BSON Object ID (OID type).

Deprecated:
Generally, it is preferred to use the append append(name, oid) method for this.
BSONObjBuilder& mongo::BSONObjBuilder::appendRegex ( const StringData fieldName,
const StringData regex,
const StringData options = "" 
) [inline]

Append a regular expression value.

Parameters:
regexthe regular expression pattern
regexoptions such as "i" or "g"
BSONObjBuilder & mongo::BSONObjBuilder::appendTimestamp ( const StringData fieldName,
unsigned long long  time,
unsigned int  inc 
) [inline]

Timestamps are a special BSON datatype that is used internally for replication.

Append a timestamp element to the object being ebuilt.

Parameters:
time- in millis (but stored in seconds)
BSONObjBuilder& mongo::BSONObjBuilder::appendTimeT ( const StringData fieldName,
time_t  dt 
) [inline]

Append a time_t date.

Parameters:
dta C-style 32 bit date value, that is the number of seconds since January 1, 1970, 00:00:00 GMT

Peek at what is in the builder, but leave the builder ready for more appends.

The returned object is only valid until the next modification or destruction of the builder. Intended use case: append a field if not already there.

Fetch the object we have built.

BSONObjBuilder still frees the object when the builder goes out of scope -- very important to keep in mind. Use obj() if you would like the BSONObj to last longer than the builder.

Generate and assign an object id for the _id field.

_id should be the first element in the object for good performance.

destructive The returned BSONObj will free the buffer when it is finished.

Returns:
owned BSONObj
BSONObjBuilderValueStream& mongo::BSONObjBuilder::operator<< ( const char *  name) [inline]

Stream oriented way to add field names and values.

BSONObjBuilder& mongo::BSONObjBuilder::operator<< ( GENOIDLabeler  ) [inline]

Stream oriented way to add field names and values.

BSONObjBuilderValueStream& mongo::BSONObjBuilder::operator<< ( const ForceExplicitString name) [inline]

Stream oriented way to add field names and values.

bool mongo::BSONObjBuilder::owned ( ) const [inline]
Returns:
true if we are using our own bufbuilder, and not an alternate that was given to us in our constructor
BufBuilder& mongo::BSONObjBuilder::subobjStart ( const StringData fieldName) [inline]

add header for a new subobject and return bufbuilder for writing to the subobject's body

example:

BSONObjBuilder b; BSONObjBuilder sub (b.subobjStart("fieldName")); // use sub sub.done() // use b and convert to object


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