Java Code Examples for com.mongodb.BasicDBObject#containsField()

The following examples show how to use com.mongodb.BasicDBObject#containsField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoFieldMapping.java    From bluima with Apache License 2.0 6 votes vote down vote up
public static void readFieldFromDb(String fieldKey, String range,
        Annotation a, Feature f, BasicDBObject dbO, JCas jCas) {

    if (dbO.containsField(fieldKey)) {

        if (range.equals("String")) {
            a.setStringValue(f, dbO.getString(fieldKey));
        } else if (range.equals("StringArray")) {
            BasicDBList vals = (BasicDBList) dbO.get(fieldKey);
            StringArray sa = new StringArray(jCas, vals.size());
            for (int i = 0; i < vals.size(); i++) {
                sa.set(i, vals.get(i).toString());
            }
            a.setFeatureValue(f, sa);
        } else if (range.equals("Integer")) {
            a.setIntValue(f, dbO.getInt(fieldKey));
        } else if (range.equals("Float")) {
            a.setFloatValue(f, (float) dbO.getDouble(fieldKey));
        } else if (range.equals("Boolean")) {
            a.setBooleanValue(f, dbO.getBoolean(fieldKey));
        } else {
            LOG.warn("range not supported " + range);
        }
    }
}
 
Example 2
Source File: QueryModel.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private static void validateAggregateCommand( DBObject commandObj ) throws OdaException
{
    // validate a $group pipeline operation expression, if specified
    List<BasicDBObject> groupOps = findPipelineOperation( commandObj, GROUP_AGGR_KEY );
    for( BasicDBObject groupOp : groupOps )
    {
        if( ! groupOp.containsField( DOC_ID_FIELD_NAME ) )
            throw new OdaException( Messages.bind( Messages.queryModel_missingGroupAggrKey, new Object[]{ GROUP_AGGR_KEY, DOC_ID_FIELD_NAME, groupOp } ) );
    }

    // validate a $sort pipeline operation expression, if specified
    List<BasicDBObject> sortOps = findPipelineOperation( commandObj, SORT_AGGR_KEY );
    for( BasicDBObject sortOp : sortOps )
    {
        for( Object sortKeySpec : sortOp.values() )
        {
            if( sortKeySpec instanceof Number )
            {
                int sortKeyValue = ((Number)sortKeySpec).intValue();
                if( sortKeyValue == 1 || sortKeyValue == -1 )
                    continue;   // is valid
            }
            throw new OdaException( Messages.bind( Messages.queryModel_invalidSortAggrValue, SORT_AGGR_KEY, sortOp ) );
        }
    }
}
 
Example 3
Source File: QueryProperties.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
BasicDBObject getSelectedFieldsAsProjectionKeys() throws OdaException
 {
     Object propValue = getPropertiesMap().get( SELECTED_FIELDS_PROP );
     if( propValue instanceof List<?> )
     {
         BasicDBObject keys = new BasicDBObject();
         @SuppressWarnings("unchecked")
         List<String> fieldNames = (List<String>) propValue;
         for( String field : fieldNames )
         {
             keys.append( field, 1 );
         }
 
         // explicitly exclude docId field if not in projected list
         if( ! keys.containsField( DOC_ID_FIELD_NAME ) )
             keys.append( DOC_ID_FIELD_NAME, 0 );
         return keys;
     }

     if( propValue instanceof String )
     {
         // user-defined projection expression
DBObject projectObj = parseExprToDBObject( (String) propValue );
if ( !( projectObj instanceof BasicDBObject ) )
{
	throw new OdaException(
			Messages.bind( "Unexpected data type ({0}) in {1}",
					projectObj.getClass( ).getSimpleName( ),
					SELECTED_FIELDS_PROP ) );
}
return (BasicDBObject) projectObj;
     }

     // non-recognized data type; log and ignore
     if( propValue != null )
         getLogger().log( Level.INFO, 
             Messages.bind( "Unexpected data type ({0}) in Selected Fields property value.", propValue.getClass().getName() )); //$NON-NLS-1$

     return new BasicDBObject();
 }