private DBObject toDBObject ( final Projection projection ) { String target ; if ( firstStage ) { MappedField field = mapper . getMappedClass ( source ) . getMappedField ( projection . getTarget ( ) ) ; target = field != null ? field . getNameToStore ( ) : projection . getTarget ( ) ; } else { target = projection . getTarget ( ) ; } if ( projection . getProjections ( ) != null ) { List < Projection > list = projection . getProjections ( ) ; DBObject projections = new BasicDBObject ( ) ; for ( Projection subProjection : list ) { projections . putAll ( toDBObject ( subProjection ) ) ; } return new BasicDBObject ( target , projections ) ; } else if ( projection . getSource ( ) != null ) { return new BasicDBObject ( target , projection . getSource ( ) ) ; } else if ( projection . getArguments ( ) != null ) { DBObject args = toExpressionArgs ( projection . getArguments ( ) ) ; if ( target == null ) { // Unwrap for single-argument expressions if ( args instanceof List < ? > && ( ( List < ? > ) args ) . size ( ) == 1 ) { Object firstArg = ( ( List < ? > ) args ) . get ( 0 ) ; if ( firstArg instanceof DBObject ) { return ( DBObject ) firstArg ; } } return args ; } else { // Unwrap for single-argument expressions if ( args instanceof List < ? > && ( ( List < ? > ) args ) . size ( ) == 1 ) { return new BasicDBObject ( target , ( ( List < ? > ) args ) . get ( 0 ) ) ; } return new BasicDBObject ( target , args ) ; } } else { return new BasicDBObject ( target , projection . isSuppressed ( ) ? 0 : 1 ) ; } }
Converts a Projection to a DBObject for use by the Java driver .