Package dev.morphia

Interface Datastore

  • All Known Subinterfaces:
    AdvancedDatastore

    public interface Datastore
    Datastore interface to get/delete/save objects
    Author:
    Scott Hernandez
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods 
      Modifier and Type Method Description
      AggregationPipeline createAggregation​(java.lang.Class source)
      Returns a new query bound to the kind (a specific DBCollection)
      <T> Query<T> createQuery​(java.lang.Class<T> collection)
      Deprecated, for removal: This API element is subject to removal in a future version.
      <T> UpdateOperations<T> createUpdateOperations​(java.lang.Class<T> clazz)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use Query.update() instead
      <T> com.mongodb.WriteResult delete​(Query<T> query)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use Query.remove() instead
      <T> com.mongodb.WriteResult delete​(Query<T> query, DeleteOptions options)
      Deprecated, for removal: This API element is subject to removal in a future version.
      <T> com.mongodb.WriteResult delete​(T entity)
      Deletes the given entity (by @Id)
      <T> com.mongodb.WriteResult delete​(T entity, DeleteOptions options)
      Deletes the given entity (by @Id), with the WriteConcern
      void enableDocumentValidation()
      Process any Validation annotations for document validation.
      void ensureCaps()
      ensure capped collections for Entity(s)
      void ensureIndexes()
      Ensures (creating if necessary) the indexes found during class mapping
      <T> void ensureIndexes​(java.lang.Class<T> clazz)
      Ensures (creating if necessary) the indexes found during class mapping
      <T> Query<T> find​(java.lang.Class<T> clazz)
      Find all instances by type
      default <T> T findAndDelete​(Query<T> query)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use Query.delete() instead
      default <T> T findAndDelete​(Query<T> query, FindAndModifyOptions options)
      Deprecated, for removal: This API element is subject to removal in a future version.
      default <T> T findAndModify​(Query<T> query, UpdateOperations<T> operations)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use Query.modify() instead
      <T,​V>
      Query<T>
      get​(java.lang.Class<T> clazz, java.lang.Iterable<V> ids)
      Deprecated.
      use Query instead.
      default <T> T get​(T entity)
      Deprecated.
      use Query instead
      <T> T getByKey​(java.lang.Class<T> clazz, Key<T> key)
      Deprecated.
      use a Query instead
      <T> java.util.List<T> getByKeys​(java.lang.Class<T> clazz, java.lang.Iterable<Key<T>> keys)
      Deprecated.
      use a Query instead
      <T> java.util.List<T> getByKeys​(java.lang.Iterable<Key<T>> keys)
      Deprecated.
      use a Query instead
      com.mongodb.DBCollection getCollection​(java.lang.Class<?> clazz)
      Deprecated.
      the return type for this method will change in 2.0
      com.mongodb.client.MongoDatabase getDatabase()  
      com.mongodb.DB getDB()
      Deprecated.
      use #getDatabase().
      <T> Key<T> getKey​(T entity)
      Creates a (type-safe) reference to the entity; if stored this will become a DBRef
      Mapper getMapper()  
      com.mongodb.MongoClient getMongo()
      Deprecated.
      no replacement is planned
      QueryFactory getQueryFactory()  
      <T> Key<T> merge​(T entity)
      Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
      <T> Key<T> merge​(T entity, com.mongodb.WriteConcern wc)
      Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
      <T> Query<T> queryByExample​(T example)
      Returns a new query based on the example object
      <T> java.lang.Iterable<Key<T>> save​(java.lang.Iterable<T> entities)
      Saves the entities (Objects) and updates the @Id field
      <T> java.lang.Iterable<Key<T>> save​(java.lang.Iterable<T> entities, InsertOptions options)
      Saves the entities (Objects) and updates the @Id field, with the WriteConcern
      <T> Key<T> save​(T entity)
      Saves an entity (Object) and updates the @Id field
      <T> Key<T> save​(T entity, InsertOptions options)
      Saves an entity (Object) and updates the @Id field
      void setQueryFactory​(QueryFactory queryFactory)
      Replaces the current QueryFactory with the given value.
      <T> UpdateResults update​(Query<T> query, UpdateOperations<T> operations)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use Query.update() instead.
      default <T> UpdateResults update​(Query<T> query, UpdateOperations<T> operations, UpdateOptions options)
      Deprecated, for removal: This API element is subject to removal in a future version.
      use Query.update() instead
    • Method Detail

      • createAggregation

        AggregationPipeline createAggregation​(java.lang.Class source)
        Returns a new query bound to the kind (a specific DBCollection)
        Parameters:
        source - The class to create aggregation against
        Returns:
        the aggregation pipeline
      • createQuery

        @Deprecated(since="2.0",
                    forRemoval=true)
        <T> Query<T> createQuery​(java.lang.Class<T> collection)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Returns a new query bound to the collection (a specific DBCollection)
        Type Parameters:
        T - the type of the query
        Parameters:
        collection - The collection to query
        Returns:
        the query
      • createUpdateOperations

        @Deprecated(since="2.0",
                    forRemoval=true)
        <T> UpdateOperations<T> createUpdateOperations​(java.lang.Class<T> clazz)
        Deprecated, for removal: This API element is subject to removal in a future version.
        use Query.update() instead
        The builder for all update operations
        Type Parameters:
        T - the type to update
        Parameters:
        clazz - the type to update
        Returns:
        the new UpdateOperations instance
      • delete

        @Deprecated(since="2.0",
                    forRemoval=true)
        <T> com.mongodb.WriteResult delete​(Query<T> query)
        Deprecated, for removal: This API element is subject to removal in a future version.
        use Query.remove() instead
        Deletes entities based on the query
        Type Parameters:
        T - the type to delete
        Parameters:
        query - the query to use when finding documents to delete
        Returns:
        results of the delete
      • delete

        @Deprecated(since="2.0",
                    forRemoval=true)
        <T> com.mongodb.WriteResult delete​(Query<T> query,
                                           DeleteOptions options)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Deletes entities based on the query
        Type Parameters:
        T - the type to delete
        Parameters:
        query - the query to use when finding documents to delete
        options - the options to apply to the delete
        Returns:
        results of the delete
        Since:
        1.3
      • delete

        <T> com.mongodb.WriteResult delete​(T entity)
        Deletes the given entity (by @Id)
        Type Parameters:
        T - the type to delete
        Parameters:
        entity - the entity to delete
        Returns:
        results of the delete
      • delete

        <T> com.mongodb.WriteResult delete​(T entity,
                                           DeleteOptions options)
        Deletes the given entity (by @Id), with the WriteConcern
        Type Parameters:
        T - the type to delete
        Parameters:
        entity - the entity to delete
        options - the options to use when deleting
        Returns:
        results of the delete
        Since:
        1.3
      • ensureCaps

        void ensureCaps()
        ensure capped collections for Entity(s)
      • enableDocumentValidation

        void enableDocumentValidation()
        Process any Validation annotations for document validation.
        Since:
        1.3
        MongoDB documentation
        core/document-validation/
      • ensureIndexes

        void ensureIndexes()
        Ensures (creating if necessary) the indexes found during class mapping
        See Also:
        Indexes, Indexed, Text
      • ensureIndexes

        <T> void ensureIndexes​(java.lang.Class<T> clazz)
        Ensures (creating if necessary) the indexes found during class mapping
        Type Parameters:
        T - the type to index
        Parameters:
        clazz - the class from which to get the index definitions
        See Also:
        Indexes, Indexed, Text
      • find

        <T> Query<T> find​(java.lang.Class<T> clazz)
        Find all instances by type
        Type Parameters:
        T - the type to query
        Parameters:
        clazz - the class to use for mapping the results
        Returns:
        the query
      • findAndDelete

        @Deprecated(since="2.0",
                    forRemoval=true)
        default <T> T findAndDelete​(Query<T> query)
        Deprecated, for removal: This API element is subject to removal in a future version.
        use Query.delete() instead
        Deletes the given entities based on the query (first item only).
        Type Parameters:
        T - the type to query
        Parameters:
        query - the query to use when finding entities to delete
        Returns:
        the deleted Entity
      • findAndDelete

        @Deprecated(since="2.0",
                    forRemoval=true)
        default <T> T findAndDelete​(Query<T> query,
                                    FindAndModifyOptions options)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Deletes the given entities based on the query (first item only).
        Type Parameters:
        T - the type to query
        Parameters:
        query - the query to use when finding entities to delete
        options - the options to apply to the delete
        Returns:
        the deleted Entity
        Since:
        1.3
      • findAndModify

        @Deprecated(since="2.0",
                    forRemoval=true)
        default <T> T findAndModify​(Query<T> query,
                                    UpdateOperations<T> operations)
        Deprecated, for removal: This API element is subject to removal in a future version.
        use Query.modify() instead
        Find the first Entity from the Query, and modify it.
        Type Parameters:
        T - the type to query
        Parameters:
        query - the query to use when finding entities to update
        operations - the updates to apply to the matched documents
        Returns:
        The modified Entity (the result of the update)
      • get

        @Deprecated
        <T,​V> Query<T> get​(java.lang.Class<T> clazz,
                                 java.lang.Iterable<V> ids)
        Deprecated.
        use Query instead.
        Find the given entities (by id); shorthand for find("_id in", ids)
        Type Parameters:
        T - the type to fetch
        V - the type of the ID
        Parameters:
        clazz - the class to use for mapping
        ids - the IDs to query
        Returns:
        the query to find the entities
        Developer note. Inline this method to update to the new usage
      • get

        @Deprecated
        default <T> T get​(T entity)
        Deprecated.
        use Query instead
        Find the given entity (by collectionName/id); think of this as refresh
        Type Parameters:
        T - the type to fetch
        Parameters:
        entity - The entity to search for
        Returns:
        the matched entity. may be null.
        Developer note. Inline this method to update to the new usage
      • getByKey

        @Deprecated
        <T> T getByKey​(java.lang.Class<T> clazz,
                       Key<T> key)
        Deprecated.
        use a Query instead
        Find the given entity (by collectionName/id);
        Type Parameters:
        T - the type to fetch
        Parameters:
        clazz - the class to use for mapping
        key - the key search with
        Returns:
        the matched entity. may be null.
      • getByKeys

        @Deprecated
        <T> java.util.List<T> getByKeys​(java.lang.Class<T> clazz,
                                        java.lang.Iterable<Key<T>> keys)
        Deprecated.
        use a Query instead
        Find the given entities (by id), verifying they are of the correct type; shorthand for find("_id in", ids)
        Type Parameters:
        T - the type to fetch
        Parameters:
        clazz - the class to use for mapping
        keys - the keys to search with
        Returns:
        the matched entities. may be null.
      • getByKeys

        @Deprecated
        <T> java.util.List<T> getByKeys​(java.lang.Iterable<Key<T>> keys)
        Deprecated.
        use a Query instead
        Find the given entities (by id); shorthand for find("_id in", ids)
        Type Parameters:
        T - the type to fetch
        Parameters:
        keys - the keys to search with
        Returns:
        the matched entities. may be null.
      • getCollection

        @Deprecated
        com.mongodb.DBCollection getCollection​(java.lang.Class<?> clazz)
        Deprecated.
        the return type for this method will change in 2.0
        Parameters:
        clazz - the class to use for mapping
        Returns:
        the mapped collection for the collection
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • getDB

        @Deprecated
        com.mongodb.DB getDB()
        Deprecated.
        use #getDatabase(). In general, should you need a DB reference, please use the MongoClient used to create this Datastore to retrieve it.
        Returns:
        the DB this Datastore uses
        See Also:
        Mongo.getDB(String), MongoDatabase
      • getDatabase

        com.mongodb.client.MongoDatabase getDatabase()
        Returns:
        the MongoDatabase used by this DataStore
        Since:
        1.5
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • getKey

        <T> Key<T> getKey​(T entity)
        Creates a (type-safe) reference to the entity; if stored this will become a DBRef
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity whose key is to be returned
        Returns:
        the Key
      • getMongo

        @Deprecated
        com.mongodb.MongoClient getMongo()
        Deprecated.
        no replacement is planned
        Get the underlying MongoClient that allows connection to the MongoDB instance being used.
        Returns:
        the MongoClient being used by this datastore.
      • setQueryFactory

        void setQueryFactory​(QueryFactory queryFactory)
        Replaces the current QueryFactory with the given value.
        Parameters:
        queryFactory - the QueryFactory to use
        See Also:
        QueryFactory
      • merge

        <T> Key<T> merge​(T entity)
        Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to merge back in to the database
        Returns:
        the key of the entity
      • merge

        <T> Key<T> merge​(T entity,
                         com.mongodb.WriteConcern wc)
        Work as if you did an update with each field in the entity doing a $set; Only at the top level of the entity.
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to merge back in to the database
        wc - the WriteConcern to use
        Returns:
        the key of the entity
      • queryByExample

        <T> Query<T> queryByExample​(T example)
        Returns a new query based on the example object
        Type Parameters:
        T - the type of the entity
        Parameters:
        example - the example entity to use when creating the query
        Returns:
        the query
      • save

        <T> java.lang.Iterable<Key<T>> save​(java.lang.Iterable<T> entities)
        Saves the entities (Objects) and updates the @Id field
        Type Parameters:
        T - the type of the entity
        Parameters:
        entities - the entities to save
        Returns:
        the keys of the entities
      • save

        <T> java.lang.Iterable<Key<T>> save​(java.lang.Iterable<T> entities,
                                            InsertOptions options)
        Saves the entities (Objects) and updates the @Id field, with the WriteConcern
        Type Parameters:
        T - the type of the entity
        Parameters:
        entities - the entities to save
        options - the options to apply to the save operation
        Returns:
        the keys of the entities
      • save

        <T> Key<T> save​(T entity)
        Saves an entity (Object) and updates the @Id field
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to save
        Returns:
        the keys of the entity
      • save

        <T> Key<T> save​(T entity,
                        InsertOptions options)
        Saves an entity (Object) and updates the @Id field
        Type Parameters:
        T - the type of the entity
        Parameters:
        entity - the entity to save
        options - the options to apply to the save operation
        Returns:
        the keys of the entity
      • update

        @Deprecated(since="2.0",
                    forRemoval=true)
        <T> UpdateResults update​(Query<T> query,
                                 UpdateOperations<T> operations)
        Deprecated, for removal: This API element is subject to removal in a future version.
        use Query.update() instead. Please note the default has changed from multi- to single- document updates.
        Updates all entities found with the operations; this is an atomic operation per entity
        Type Parameters:
        T - the type of the entity
        Parameters:
        query - the query used to match the documents to update
        operations - the update operations to perform
        Returns:
        the results of the updates
      • update

        @Deprecated(since="2.0",
                    forRemoval=true)
        default <T> UpdateResults update​(Query<T> query,
                                         UpdateOperations<T> operations,
                                         UpdateOptions options)
        Deprecated, for removal: This API element is subject to removal in a future version.
        use Query.update() instead
        Updates all entities found with the operations; this is an atomic operation per entity
        Type Parameters:
        T - the type of the entity
        Parameters:
        query - the query used to match the documents to update
        operations - the update operations to perform
        options - the options to apply to the update
        Returns:
        the results of the updates
        Since:
        1.3
      • getMapper

        Mapper getMapper()
        Returns:
        the Mapper used by this Datastore
        Since:
        1.5
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.