Class MongoDbDataContext

  • All Implemented Interfaces:
    org.apache.metamodel.convert.HasReadTypeConverters, org.apache.metamodel.DataContext, org.apache.metamodel.UpdateableDataContext

    public class MongoDbDataContext
    extends org.apache.metamodel.QueryPostprocessDataContext
    implements org.apache.metamodel.UpdateableDataContext
    DataContext implementation for MongoDB. Since MongoDB has no schema, a virtual schema will be used in this DataContext. This implementation supports either automatic discovery of a schema or manual specification of a schema, through the SimpleTableDef class.
    • Field Summary

      • Fields inherited from class org.apache.metamodel.QueryPostprocessDataContext

        INFORMATION_SCHEMA_NAME, SYSTEM_PROPERTY_CREATE_DEFAULT_TABLE_ALIAS
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addTable​(org.apache.metamodel.schema.MutableTable table)  
      protected org.bson.Document createMongoDbQuery​(org.apache.metamodel.schema.Table table, List<org.apache.metamodel.query.FilterItem> whereItems, Consumer<org.apache.metamodel.query.FilterItem> whereItemToPostProcessConsumer)  
      static org.apache.metamodel.util.SimpleTableDef[] detectSchema​(com.mongodb.client.MongoDatabase mongoDb)
      Performs an analysis of the available collections in a Mongo DB instance and tries to detect the table's structure based on the first 1000 documents in each collection.
      static org.apache.metamodel.util.SimpleTableDef detectTable​(com.mongodb.client.MongoDatabase mongoDb, String collectionName)
      Performs an analysis of an available collection in a Mongo DB instance and tries to detect the table structure based on the first 1000 documents in the collection.
      protected Number executeCountQuery​(org.apache.metamodel.schema.Table table, List<org.apache.metamodel.query.FilterItem> whereItems, boolean functionApproximationAllowed)  
      protected org.apache.metamodel.data.Row executePrimaryKeyLookupQuery​(org.apache.metamodel.schema.Table table, List<org.apache.metamodel.query.SelectItem> selectItems, org.apache.metamodel.schema.Column primaryKeyColumn, Object keyValue)  
      org.apache.metamodel.data.DataSet executeQuery​(org.apache.metamodel.query.Query query)  
      org.apache.metamodel.UpdateSummary executeUpdate​(org.apache.metamodel.UpdateScript update)  
      org.apache.metamodel.UpdateSummary executeUpdate​(org.apache.metamodel.UpdateScript update, com.mongodb.WriteConcern writeConcern)
      Executes an update with a specific WriteConcern.
      org.apache.metamodel.UpdateSummary executeUpdate​(org.apache.metamodel.UpdateScript update, WriteConcernAdvisor writeConcernAdvisor)
      Executes an update with a specific WriteConcernAdvisor.
      protected org.apache.metamodel.schema.Schema getMainSchema()  
      protected String getMainSchemaName()  
      com.mongodb.client.MongoDatabase getMongoDb()
      Gets the DB instance that this DataContext is backed by.
      WriteConcernAdvisor getWriteConcernAdvisor()
      protected org.apache.metamodel.data.DataSet materializeMainSchemaTable​(org.apache.metamodel.schema.Table table, List<org.apache.metamodel.schema.Column> columns, int maxRows)  
      protected org.apache.metamodel.data.DataSet materializeMainSchemaTable​(org.apache.metamodel.schema.Table table, List<org.apache.metamodel.schema.Column> columns, int firstRow, int maxRows)  
      void setWriteConcernAdvisor​(WriteConcernAdvisor writeConcernAdvisor)
      Sets a global WriteConcern advisor to use on executeUpdate(UpdateScript).
      • Methods inherited from class org.apache.metamodel.QueryPostprocessDataContext

        addConverter, getDefaultSchemaName, getSchemaByNameInternal, getSchemaNamesInternal, isMainSchemaTable, isScalarFunctionMaterialized, materializeFromItem, materializeMainSchemaTable, materializeMainSchemaTableSelect, materializeTable
      • Methods inherited from class org.apache.metamodel.AbstractDataContext

        compileQuery, executeQuery, executeQuery, getColumnByQualifiedLabel, getDefaultSchema, getSchemaByName, getSchemaNames, getSchemas, getTableByQualifiedLabel, isQualifiedPathDelim, onSchemaCacheRefreshed, parseQuery, query, refreshSchemas
      • Methods inherited from interface org.apache.metamodel.DataContext

        compileQuery, executeQuery, executeQuery, getColumnByQualifiedLabel, getDefaultSchema, getSchemaByName, getSchemaNames, getSchemas, getTableByQualifiedLabel, parseQuery, query, refreshSchemas
    • Constructor Detail

      • MongoDbDataContext

        public MongoDbDataContext​(com.mongodb.client.MongoDatabase mongoDb,
                                  org.apache.metamodel.util.SimpleTableDef... tableDefs)
        Constructs a MongoDbDataContext. This constructor accepts a custom array of SimpleTableDefs which allows the user to define his own view on the collections in the database.
        Parameters:
        mongoDb - the mongo db connection
        tableDefs - an array of SimpleTableDefs, which define the table and column model of the mongo db collections. (consider using detectSchema(MongoDatabase) or detectTable(MongoDatabase, String) ).
      • MongoDbDataContext

        public MongoDbDataContext​(com.mongodb.client.MongoDatabase mongoDb)
        Constructs a MongoDbDataContext and automatically detects the schema structure/view on all collections (see detectSchema(MongoDatabase)).
        Parameters:
        mongoDb - the mongo db connection
    • Method Detail

      • detectSchema

        public static org.apache.metamodel.util.SimpleTableDef[] detectSchema​(com.mongodb.client.MongoDatabase mongoDb)
        Performs an analysis of the available collections in a Mongo DB instance and tries to detect the table's structure based on the first 1000 documents in each collection.
        Parameters:
        mongoDb - the mongo db to inspect
        Returns:
        a mutable schema instance, useful for further fine tuning by the user.
        See Also:
        detectTable(MongoDatabase, String)
      • detectTable

        public static org.apache.metamodel.util.SimpleTableDef detectTable​(com.mongodb.client.MongoDatabase mongoDb,
                                                                           String collectionName)
        Performs an analysis of an available collection in a Mongo DB instance and tries to detect the table structure based on the first 1000 documents in the collection.
        Parameters:
        mongoDb - the mongo DB
        collectionName - the name of the collection
        Returns:
        a table definition for mongo db.
      • getMainSchema

        protected org.apache.metamodel.schema.Schema getMainSchema()
                                                            throws org.apache.metamodel.MetaModelException
        Specified by:
        getMainSchema in class org.apache.metamodel.QueryPostprocessDataContext
        Throws:
        org.apache.metamodel.MetaModelException
      • getMainSchemaName

        protected String getMainSchemaName()
                                    throws org.apache.metamodel.MetaModelException
        Specified by:
        getMainSchemaName in class org.apache.metamodel.QueryPostprocessDataContext
        Throws:
        org.apache.metamodel.MetaModelException
      • executeCountQuery

        protected Number executeCountQuery​(org.apache.metamodel.schema.Table table,
                                           List<org.apache.metamodel.query.FilterItem> whereItems,
                                           boolean functionApproximationAllowed)
        Overrides:
        executeCountQuery in class org.apache.metamodel.QueryPostprocessDataContext
      • executePrimaryKeyLookupQuery

        protected org.apache.metamodel.data.Row executePrimaryKeyLookupQuery​(org.apache.metamodel.schema.Table table,
                                                                             List<org.apache.metamodel.query.SelectItem> selectItems,
                                                                             org.apache.metamodel.schema.Column primaryKeyColumn,
                                                                             Object keyValue)
        Overrides:
        executePrimaryKeyLookupQuery in class org.apache.metamodel.QueryPostprocessDataContext
      • executeQuery

        public org.apache.metamodel.data.DataSet executeQuery​(org.apache.metamodel.query.Query query)
        Specified by:
        executeQuery in interface org.apache.metamodel.DataContext
        Overrides:
        executeQuery in class org.apache.metamodel.QueryPostprocessDataContext
      • createMongoDbQuery

        protected org.bson.Document createMongoDbQuery​(org.apache.metamodel.schema.Table table,
                                                       List<org.apache.metamodel.query.FilterItem> whereItems,
                                                       Consumer<org.apache.metamodel.query.FilterItem> whereItemToPostProcessConsumer)
      • materializeMainSchemaTable

        protected org.apache.metamodel.data.DataSet materializeMainSchemaTable​(org.apache.metamodel.schema.Table table,
                                                                               List<org.apache.metamodel.schema.Column> columns,
                                                                               int maxRows)
        Specified by:
        materializeMainSchemaTable in class org.apache.metamodel.QueryPostprocessDataContext
      • materializeMainSchemaTable

        protected org.apache.metamodel.data.DataSet materializeMainSchemaTable​(org.apache.metamodel.schema.Table table,
                                                                               List<org.apache.metamodel.schema.Column> columns,
                                                                               int firstRow,
                                                                               int maxRows)
        Overrides:
        materializeMainSchemaTable in class org.apache.metamodel.QueryPostprocessDataContext
      • executeUpdate

        public org.apache.metamodel.UpdateSummary executeUpdate​(org.apache.metamodel.UpdateScript update,
                                                                WriteConcernAdvisor writeConcernAdvisor)
        Executes an update with a specific WriteConcernAdvisor.
      • executeUpdate

        public org.apache.metamodel.UpdateSummary executeUpdate​(org.apache.metamodel.UpdateScript update,
                                                                com.mongodb.WriteConcern writeConcern)
        Executes an update with a specific WriteConcern.
      • executeUpdate

        public org.apache.metamodel.UpdateSummary executeUpdate​(org.apache.metamodel.UpdateScript update)
        Specified by:
        executeUpdate in interface org.apache.metamodel.UpdateableDataContext
      • getMongoDb

        public com.mongodb.client.MongoDatabase getMongoDb()
        Gets the DB instance that this DataContext is backed by.
        Returns:
      • addTable

        protected void addTable​(org.apache.metamodel.schema.MutableTable table)