0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

some BSONObjBuilder helpers for accessing data whilebuilding

This commit is contained in:
Eliot Horowitz 2011-01-21 12:58:20 -05:00
parent eb6e8a111c
commit 34de75445d
2 changed files with 33 additions and 0 deletions

View File

@ -121,6 +121,26 @@ namespace mongo {
return *this;
}
/* add all the fields from the object specified to this object if they don't exist */
inline BSONObjBuilder& BSONObjBuilder::appendElementsUnique(BSONObj x) {
set<string> have;
{
BSONObjIterator i = iterator();
while ( i.more() )
have.insert( i.next().fieldName() );
}
BSONObjIterator it(x);
while ( it.more() ) {
BSONElement e = it.next();
if ( have.count( e.fieldName() ) )
continue;
append(e);
}
return *this;
}
inline bool BSONObj::isValid() {
int x = objsize();
return x > 0 && x <= BSONObjMaxInternalSize;
@ -205,6 +225,14 @@ namespace mongo {
return BSONObjIterator( s , e );
}
inline bool BSONObjBuilder::hasField( const StringData& name ) const {
BSONObjIterator i = iterator();
while ( i.more() )
if ( strcmp( name.data() , i.next().fieldName() ) == 0 )
return true;
return false;
}
/* WARNING: nested/dotted conversions are not 100% reversible
* nested2dotted(dotted2nested({a.b: {c:1}})) -> {a.b.c: 1}
* also, dotted2nested ignores order

View File

@ -104,6 +104,9 @@ namespace mongo {
/** add all the fields from the object specified to this object */
BSONObjBuilder& appendElements(BSONObj x);
/** add all the fields from the object specified to this object if they don't exist already */
BSONObjBuilder& appendElementsUnique( BSONObj x );
/** append element to the object we are building */
BSONObjBuilder& append( const BSONElement& e) {
assert( !e.eoo() ); // do not append eoo, that would corrupt us. the builder auto appends when done() is called.
@ -616,6 +619,8 @@ namespace mongo {
BSONObjIterator iterator() const ;
bool hasField( const StringData& name ) const ;
int len() const { return _b.len(); }
private: