Beispiel #1
0
/* well ordered compare */
int BSONObj::woSortOrder(const BSONObj& other, const BSONObj& sortKey , bool useDotted ) const {
    if ( isEmpty() )
        return other.isEmpty() ? 0 : -1;
    if ( other.isEmpty() )
        return 1;

    uassert( 10060 ,  "woSortOrder needs a non-empty sortKey" , ! sortKey.isEmpty() );

    BSONObjIterator i(sortKey);
    while ( 1 ) {
        BSONElement f = i.next();
        if ( f.eoo() )
            return 0;

        BSONElement l = useDotted ? getFieldDotted( f.fieldName() ) : getField( f.fieldName() );
        if ( l.eoo() )
            l = staticNull.firstElement();
        BSONElement r = useDotted ? other.getFieldDotted( f.fieldName() ) : other.getField( f.fieldName() );
        if ( r.eoo() )
            r = staticNull.firstElement();

        int x = l.woCompare( r, false );
        if ( f.number() < 0 )
            x = -x;
        if ( x != 0 )
            return x;
    }
    return -1;
}
Beispiel #2
0
 BSONObj BSONObj::extractFields(const BSONObj& pattern) const {
     BSONObjBuilder b(32); // scanandorder.h can make a zillion of these, so we start the allocation very small
     BSONObjIterator i(pattern);
     while ( i.more() ) {
         BSONElement e = i.next();
         if ( e.eoo() )
             break;
         BSONElement x = getFieldDotted(e.fieldName());
         if ( x.eoo() )
             return BSONObj();
         b.append(x);
     }
     return b.obj();
 }
Beispiel #3
0
 BSONObj BSONObj::extractFields(const BSONObj& pattern , bool fillWithNull ) const {
     BSONObjBuilder b(32); // scanandorder.h can make a zillion of these, so we start the allocation very small
     BSONObjIterator i(pattern);
     while ( i.moreWithEOO() ) {
         BSONElement e = i.next();
         if ( e.eoo() )
             break;
         BSONElement x = getFieldDotted(e.fieldName());
         if ( ! x.eoo() )
             b.appendAs( x, e.fieldName() );
         else if ( fillWithNull )
             b.appendNull( e.fieldName() );
     }
     return b.obj();
 }
Beispiel #4
0
    /* makes a new BSONObj with the fields specified in pattern.
       fields returned in the order they appear in pattern.
       if any field missing or undefined in the original object, that field
       in the output will be null.

       n^2 implementation bad if pattern and object have lots
       of fields - normally pattern doesn't so should be fine.
    */
    BSONObj BSONObj::extractFieldsDotted(BSONObj pattern) const {
        BSONObjBuilder b;
        BSONObjIterator i(pattern);
        while (i.more()) {
            BSONElement e = i.next();
            const char *name = e.fieldName();

            BSONElement x = getFieldDotted( name );
            if ( x.eoo() || x.type() == Undefined ) {
                b.appendNull(name);
            } else {
                b.appendAs(x, name);
            }
        }
        return b.done();
    }
Beispiel #5
0
    BSONObj ClientCursor::extractFields(const BSONObj &pattern , bool fillWithNull ) {
        BSONObjBuilder b( pattern.objsize() * 2 );
     
        BSONObjIterator i( pattern ); 
        while ( i.more() ) {
            BSONElement key = i.next();
            BSONElement value = getFieldDotted( key.fieldName() );

            if ( value.type() ) {
                b.append( value );
                continue;
            }

            if ( fillWithNull ) 
                b.appendNull( key.fieldName() );            
            
        }

        return b.obj();
    }