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:
parent
eb6e8a111c
commit
34de75445d
@ -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
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user