26 _builders.push_back( std::make_pair(
"", b ) );
30 void prepareContext( std::string &name ) {
31 int i = 1, n = _builders.size();
33 name.substr( 0, _builders[ i ].first.length() ) == _builders[ i ].first &&
34 ( name[ _builders[i].first.length() ] ==
'.' || name[ _builders[i].first.length() ] == 0 )
36 name = name.substr( _builders[ i ].first.length() + 1 );
39 for(
int j = n - 1; j >= i; --j ) {
42 for( std::string next = splitDot( name ); !next.empty(); next = splitDot( name ) ) {
46 void appendAs(
const BSONElement &e, std::string name ) {
48 std::string dummyName = name +
".foo";
49 prepareContext( dummyName );
52 prepareContext( name );
55 BufBuilder &subarrayStartAs( std::string name ) {
56 prepareContext( name );
60 while( ! _builderStorage.empty() )
64 static std::string splitDot( std::string & str ) {
65 size_t pos = str.find(
'.' );
66 if ( pos == std::string::npos )
68 std::string ret = str.substr( 0, pos );
69 str = str.substr( pos + 1 );
74 void addBuilder(
const std::string &name ) {
75 boost::shared_ptr< BSONObjBuilder > newBuilder(
new BSONObjBuilder( back()->subobjStart( name ) ) );
76 _builders.push_back( std::make_pair( name, newBuilder.get() ) );
77 _builderStorage.push_back( newBuilder );
82 _builderStorage.pop_back();
87 std::vector< std::pair< std::string, BSONObjBuilder * > > _builders;
88 std::vector< boost::shared_ptr< BSONObjBuilder > > _builderStorage;
BSONElement represents an "element" in a BSONObj.
Definition bsonelement.h:55
int valuesize() const
size in bytes of the element's value (when applicable).
Definition bsonelement.h:166
BSONType type() const
Returns the type of the element.
Definition bsonelement.h:109
Utility for creating a BSONObj.
Definition bsonobjbuilder.h:53
BufBuilder & subarrayStart(const StringData &fieldName)
add header for a new subarray and return bufbuilder for writing to the subarray's body
Definition bsonobjbuilder.h:199
BSONObjBuilder & appendAs(const BSONElement &e, const StringData &fieldName)
append an element but with a new name
Definition bsonobjbuilder.h:131
BSONObj done()
Fetch the object we have built.
Definition bsonobjbuilder.h:623
Definition embedded_builder.h:23
the main MongoDB namespace
Definition bulk_operation_builder.h:24
@ Object
an embedded object
Definition bsontypes.h:48