Interface Query<T>

  • Type Parameters:
    T - The java type to query against
    All Known Implementing Classes:
    QueryImpl

    public interface Query<T>
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods 
      Modifier and Type Method Description
      CriteriaContainer and​(Criteria... criteria)
      Creates a container to hold 'and' clauses
      Query<T> batchSize​(int value)
      Deprecated.
      use the methods that accept Options directly
      long count()
      Count the total number of values in the result, ignoring limit and offset
      long count​(CountOptions options)
      Count the total number of values in the result, ignoring limit and offset
      FieldEnd<? extends CriteriaContainer> criteria​(java.lang.String field)
      Creates a criteria to apply against a field
      T delete()  
      T delete​(FindAndModifyOptions options)  
      Query<T> disableValidation()
      Turns off validation (for all calls made after)
      Query<T> enableValidation()
      Turns on validation (for all calls made after); by default validation is on
      dev.morphia.query.internal.MorphiaCursor<T> execute()
      Execute the query and get the results.
      dev.morphia.query.internal.MorphiaCursor<T> execute​(FindOptions options)
      Execute the query and get the results.
      java.util.Map<java.lang.String,​java.lang.Object> explain()
      Provides information on the query plan.
      java.util.Map<java.lang.String,​java.lang.Object> explain​(FindOptions options)
      Provides information on the query plan.
      FieldEnd<? extends Query<T>> field​(java.lang.String field)
      Fluent query interface: createQuery(Ent.class).field("count").greaterThan(7)...
      Query<T> filter​(java.lang.String condition, java.lang.Object value)
      Create a filter based on the specified condition and value.
      default dev.morphia.query.internal.MorphiaCursor<T> find()
      Deprecated, for removal: This API element is subject to removal in a future version.
      default dev.morphia.query.internal.MorphiaCursor<T> find​(FindOptions options)
      Deprecated, for removal: This API element is subject to removal in a future version.
      T first()
      Gets the first entity in the result set.
      T first​(FindOptions options)
      Gets the first entity in the result set.
      int getBatchSize()
      Deprecated.
      use the methods that accept Options directly
      Key<T> getKey()
      Deprecated.
      use first() instead
      Key<T> getKey​(FindOptions options)
      Deprecated.
      use first() instead
      int getOffset()
      Deprecated.
      use the methods that accept Options directly
      Query<T> hintIndex​(java.lang.String idxName)
      Deprecated.
      use the methods that accept Options directly.
      dev.morphia.query.internal.MorphiaKeyCursor<T> keys()
      Execute the query and get the results (as a MorphiaCursor<Key<T>>)
      dev.morphia.query.internal.MorphiaKeyCursor<T> keys​(FindOptions options)
      Execute the query and get the results (as a MorphiaCursor<Key<T>>)
      Query<T> limit​(int value)
      Deprecated.
      use the methods that accept Options directly
      Query<T> lowerIndexBound​(com.mongodb.DBObject lowerBound)
      Deprecated.
      use the methods that accept Options directly.
      Query<T> maxScan​(int value)
      Deprecated.
      use the methods that accept Options directly.
      Query<T> maxTime​(long maxTime, java.util.concurrent.TimeUnit maxTimeUnit)
      Deprecated.
      use the methods that accept Options directly.
      Modify<T> modify()  
      Modify<T> modify​(UpdateOperations<T> operations)
      Deprecated. 
      Query<T> offset​(int value)
      Deprecated.
      use the methods that accept Options directly
      CriteriaContainer or​(Criteria... criteria)
      Creates a container to hold 'or' clauses
      Query<T> order​(Meta sort)
      Sorts based on a metadata (defines return order).
      Query<T> order​(Sort... sorts)
      Sorts based on a specified sort keys (defines return order).
      Query<T> project​(Meta meta)
      Adds a metadata field to a projection.
      Query<T> project​(java.lang.String field, boolean include)
      Adds a field to the projection clause.
      Query<T> project​(java.lang.String field, ArraySlice slice)
      Adds an sliced array field to a projection.
      Query<T> queryNonPrimary()
      Deprecated.
      use the methods that accept Options directly
      Query<T> queryPrimaryOnly()
      Deprecated.
      use the methods that accept Options directly.
      default com.mongodb.WriteResult remove()  
      com.mongodb.WriteResult remove​(DeleteOptions options)  
      Query<T> retrievedFields​(boolean include, java.lang.String... fields)
      Deprecated.
      Query<T> retrieveKnownFields()
      Limits the fields retrieved to those of the query type -- dangerous with interfaces and abstract classes
      Query<T> returnKey()
      Deprecated.
      use the methods that accept Options directly.
      Query<T> search​(java.lang.String text)
      Perform a text search on the content of the fields indexed with a text index..
      Query<T> search​(java.lang.String text, java.lang.String language)
      Perform a text search on the content of the fields indexed with a text index..
      QueryImpl.Update update()  
      QueryImpl.Update update​(com.mongodb.DBObject dbObject)  
      QueryImpl.Update update​(UpdateOperations operations)
      Deprecated, for removal: This API element is subject to removal in a future version. 
      Query<T> upperIndexBound​(com.mongodb.DBObject upperBound)
      Deprecated.
      use the methods that accept Options directly.
      Query<T> useReadPreference​(com.mongodb.ReadPreference readPref)
      Deprecated.
      use the methods that accept Options directly
      Query<T> where​(java.lang.String js)
      Limit the query using this javascript block; only one per query
      Query<T> where​(org.bson.types.CodeWScope js)
      Limit the query using this javascript block; only one per query
    • Method Detail

      • and

        CriteriaContainer and​(Criteria... criteria)
        Creates a container to hold 'and' clauses
        Parameters:
        criteria - the clauses to 'and' together
        Returns:
        the container
      • batchSize

        @Deprecated
        Query<T> batchSize​(int value)
        Deprecated.
        use the methods that accept Options directly
        Batch-size of the fetched result (cursor).
        Parameters:
        value - must be >= 0. A value of 0 indicates the server default.
        Returns:
        this
        See Also:
        FindOptions.batchSize(int)
      • criteria

        FieldEnd<? extends CriteriaContainer> criteria​(java.lang.String field)
        Creates a criteria to apply against a field
        Parameters:
        field - the field
        Returns:
        the FieldEnd to define the criteria
      • disableValidation

        Query<T> disableValidation()
        Turns off validation (for all calls made after)
        Returns:
        this
      • enableValidation

        Query<T> enableValidation()
        Turns on validation (for all calls made after); by default validation is on
        Returns:
        this
      • explain

        java.util.Map<java.lang.String,​java.lang.Object> explain()
        Provides information on the query plan. The query plan is the plan the server uses to find the matches for a query. This information may be useful when optimizing a query.
        Returns:
        Map describing the process used to return the query results.
        MongoDB documentation
        reference/operator/meta/explain/ explain
      • explain

        java.util.Map<java.lang.String,​java.lang.Object> explain​(FindOptions options)
        Provides information on the query plan. The query plan is the plan the server uses to find the matches for a query. This information may be useful when optimizing a query.
        Parameters:
        options - the options to apply to the explain operation
        Returns:
        Map describing the process used to return the query results.
        Since:
        1.3
        MongoDB documentation
        reference/operator/meta/explain/ explain
      • field

        FieldEnd<? extends Query<T>> field​(java.lang.String field)
        Fluent query interface: createQuery(Ent.class).field("count").greaterThan(7)...
        Parameters:
        field - the field
        Returns:
        the FieldEnd to define the criteria
      • filter

        Query<T> filter​(java.lang.String condition,
                        java.lang.Object value)
        Create a filter based on the specified condition and value.

        Note: Property is in the form of "name op" ("age >").

        Valid operators are ["=", "==","!=", "<>", ">", "<", ">=", "<=", "in", "nin", "all", "size", "exists"]

        Examples:

        • filter("yearsOfOperation >", 5)
        • filter("rooms.maxBeds >=", 2)
        • filter("rooms.bathrooms exists", 1)
        • filter("stars in", new Long[]{3, 4}) //3 and 4 stars (midrange?)
        • filter("quantity mod", new Long[]{4, 0}) // customers ordered in packs of 4)
        • filter("age >=", age)
        • filter("age =", age)
        • filter("age", age) (if no operator, = is assumed)
        • filter("age !=", age)
        • filter("age in", ageList)
        • filter("customers.loyaltyYears in", yearsList)

        You can filter on id properties if this query is restricted to a Class.

        Parameters:
        condition - the condition to apply
        value - the value to apply against
        Returns:
        this
      • getOffset

        @Deprecated
        int getOffset()
        Deprecated.
        use the methods that accept Options directly
        Returns:
        the offset.
        See Also:
        offset(int), FindOptions.getSkip()
      • hintIndex

        @Deprecated
        Query<T> hintIndex​(java.lang.String idxName)
        Deprecated.
        use the methods that accept Options directly. This can be replicated with options.modifier("$hint", idxName)
        Hints as to which index should be used.
        Parameters:
        idxName - the index name to hint
        Returns:
        this
        See Also:
        FindOptions.modifier(String, Object)
      • limit

        @Deprecated
        Query<T> limit​(int value)
        Deprecated.
        use the methods that accept Options directly
        Limit the fetched result set to a certain number of values.
        Parameters:
        value - must be >= 0. A value of 0 indicates no limit. For values < 0, use FindOptions.batchSize(int) which is the preferred method
        Returns:
        this
        See Also:
        FindOptions.limit(int)
      • lowerIndexBound

        @Deprecated
        Query<T> lowerIndexBound​(com.mongodb.DBObject lowerBound)
        Deprecated.
        use the methods that accept Options directly. This can be replicated using options.modifier("$min", new Document(...))

        Specify the inclusive lower bound for a specific index in order to constrain the results of this query.

        You can chain key/value pairs to build a constraint for a compound index. For instance:

        query.lowerIndexBound(new BasicDBObject("a", 1).append("b", 2));

        to build a constraint on index {"a", "b"}

        Parameters:
        lowerBound - The inclusive lower bound.
        Returns:
        this
        See Also:
        FindOptions.modifier(String, Object)
        MongoDB documentation
        reference/operator/meta/min/ $min
      • maxTime

        @Deprecated
        Query<T> maxTime​(long maxTime,
                         java.util.concurrent.TimeUnit maxTimeUnit)
        Deprecated.
        use the methods that accept Options directly. This can be replicated using options.maxTime(value, unit)
        Specifies a time limit for executing the query. Requires server version 2.6 or above.
        Parameters:
        maxTime - must be > 0. A value < 0 indicates no limit
        maxTimeUnit - the unit of time to use
        Returns:
        this
        See Also:
        FindOptions.modifier(String, Object)
      • offset

        @Deprecated
        Query<T> offset​(int value)
        Deprecated.
        use the methods that accept Options directly
        Starts the query results at a particular zero-based offset.
        Parameters:
        value - must be >= 0
        Returns:
        this
        See Also:
        FindOptions.skip(int)
      • or

        CriteriaContainer or​(Criteria... criteria)
        Creates a container to hold 'or' clauses
        Parameters:
        criteria - the clauses to 'or' together
        Returns:
        the container
      • order

        Query<T> order​(Meta sort)
        Sorts based on a metadata (defines return order). Example: order(Meta.textScore()) ({textScore : { $meta: "textScore" }})
        Parameters:
        sort - the sort order to apply
        Returns:
        this
      • order

        Query<T> order​(Sort... sorts)
        Sorts based on a specified sort keys (defines return order).
        Parameters:
        sorts - the sort order to apply
        Returns:
        this
      • project

        Query<T> project​(java.lang.String field,
                         boolean include)
        Adds a field to the projection clause. Passing true for include will include the field in the results. Projected fields must all be inclusions or exclusions. You can not include and exclude fields at the same time with the exception of the _id field. The _id field is always included unless explicitly suppressed.
        Parameters:
        field - the field to project
        include - true to include the field in the results
        Returns:
        this
        See Also:
        Project Fields to Return from Query
      • queryNonPrimary

        @Deprecated
        Query<T> queryNonPrimary()
        Deprecated.
        use the methods that accept Options directly
        Route query to non-primary node
        Returns:
        this
        See Also:
        ReadPreference.secondary(), ReadPreference.secondaryPreferred(), FindOptions.readPreference(ReadPreference), ReadPreference.secondary(), ReadPreference.secondaryPreferred()
      • queryPrimaryOnly

        @Deprecated
        Query<T> queryPrimaryOnly()
        Deprecated.
        use the methods that accept Options directly.
        Route query to primary node
        Returns:
        this
        See Also:
        ReadPreference.primary(), FindOptions.readPreference(ReadPreference), ReadPreference.primary(), ReadPreference.primaryPreferred()
      • retrieveKnownFields

        Query<T> retrieveKnownFields()
        Limits the fields retrieved to those of the query type -- dangerous with interfaces and abstract classes
        Returns:
        this
      • retrievedFields

        @Deprecated
        Query<T> retrievedFields​(boolean include,
                                 java.lang.String... fields)
        Deprecated.
        Limits the fields retrieved
        Parameters:
        include - true if the fields should be included in the results. false to exclude them.
        fields - the fields in question
        Returns:
        this
      • returnKey

        @Deprecated
        Query<T> returnKey()
        Deprecated.
        use the methods that accept Options directly. This can be replicated using options.modifier("$returnKey", true)
        Only return the index field or fields for the results of the query. If $returnKey is set to true and the query does not use an index to perform the read operation, the returned documents will not contain any fields
        Returns:
        the Query to enable chaining of commands
        See Also:
        FindOptions.modifier(String, Object)
        MongoDB documentation
        reference/operator/meta/returnKey/#op._S_returnKey $returnKey
      • search

        Query<T> search​(java.lang.String text)
        Perform a text search on the content of the fields indexed with a text index..
        Parameters:
        text - the text to search for
        Returns:
        the Query to enable chaining of commands
        MongoDB documentation
        reference/operator/query/text/ $text
      • search

        Query<T> search​(java.lang.String text,
                        java.lang.String language)
        Perform a text search on the content of the fields indexed with a text index..
        Parameters:
        text - the text to search for
        language - the language to use during the search
        Returns:
        the Query to enable chaining of commands
        MongoDB documentation
        reference/operator/query/text/ $text
      • upperIndexBound

        @Deprecated
        Query<T> upperIndexBound​(com.mongodb.DBObject upperBound)
        Deprecated.
        use the methods that accept Options directly. This can be replicated using options.modifier("$max", new Document(...))

        Specify the exclusive upper bound for a specific index in order to constrain the results of this query.

        You can chain key/value pairs to build a constraint for a compound index. For instance:

        query.upperIndexBound(new BasicDBObject("a", 1).append("b", 2));

        to build a constraint on index {"a", "b"}

        Parameters:
        upperBound - The exclusive upper bound.
        Returns:
        this
        See Also:
        FindOptions.modifier(String, Object)
        MongoDB documentation
        reference/operator/meta/max/ $max
      • useReadPreference

        @Deprecated
        Query<T> useReadPreference​(com.mongodb.ReadPreference readPref)
        Deprecated.
        use the methods that accept Options directly
        Updates the ReadPreference to use
        Parameters:
        readPref - the ReadPreference to use
        Returns:
        this
        See Also:
        ReadPreference, FindOptions.readPreference(ReadPreference)
      • where

        Query<T> where​(java.lang.String js)
        Limit the query using this javascript block; only one per query
        Parameters:
        js - the javascript block to apply
        Returns:
        this
      • where

        Query<T> where​(org.bson.types.CodeWScope js)
        Limit the query using this javascript block; only one per query
        Parameters:
        js - the javascript block to apply
        Returns:
        this
      • keys

        dev.morphia.query.internal.MorphiaKeyCursor<T> keys()
        Execute the query and get the results (as a MorphiaCursor<Key<T>>)
        Returns:
        the keys of the documents returned by this query
      • keys

        dev.morphia.query.internal.MorphiaKeyCursor<T> keys​(FindOptions options)
        Execute the query and get the results (as a MorphiaCursor<Key<T>>)
        Parameters:
        options - the options to apply to the find operation
        Returns:
        the keys of the documents returned by this query
        Since:
        1.4
      • count

        long count()
        Count the total number of values in the result, ignoring limit and offset
        Returns:
        the count
        Since:
        1.3
      • count

        long count​(CountOptions options)
        Count the total number of values in the result, ignoring limit and offset
        Parameters:
        options - the options to apply to the count operation
        Returns:
        the count
        Since:
        1.3
      • execute

        dev.morphia.query.internal.MorphiaCursor<T> execute()
        Execute the query and get the results. *note* the return type of this will change in 2.0.
        Returns:
        a MorphiaCursor
        Since:
        1.4
        See Also:
        execute(FindOptions)
      • execute

        dev.morphia.query.internal.MorphiaCursor<T> execute​(FindOptions options)
        Execute the query and get the results.
        Parameters:
        options - the options to apply to the find operation
        Returns:
        a MorphiaCursor
        Since:
        1.4
      • find

        @Deprecated(since="2.0",
                    forRemoval=true)
        default dev.morphia.query.internal.MorphiaCursor<T> find()
        Deprecated, for removal: This API element is subject to removal in a future version.
        Execute the query and get the results. *note* the return type of this will change in 2.0.
        Returns:
        a MorphiaCursor
        Since:
        1.4
        See Also:
        execute(FindOptions)
      • find

        @Deprecated(since="2.0",
                    forRemoval=true)
        default dev.morphia.query.internal.MorphiaCursor<T> find​(FindOptions options)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Execute the query and get the results.
        Parameters:
        options - the options to apply to the find operation
        Returns:
        a MorphiaCursor
        Since:
        1.4
      • first

        T first()
        Gets the first entity in the result set. Obeys the Query offset value.
        Returns:
        the only instance in the result, or null if the result set is empty.
        Since:
        1.5
      • first

        T first​(FindOptions options)
        Gets the first entity in the result set. Obeys the Query offset value.
        Parameters:
        options - the options to apply to the find operation
        Returns:
        the only instance in the result, or null if the result set is empty.
        Since:
        1.5
      • getKey

        @Deprecated
        Key<T> getKey()
        Deprecated.
        use first() instead
        Get the key of the first entity in the result set. Obeys the Query offset value.
        Returns:
        the key of the first instance in the result, or null if the result set is empty.
      • getKey

        @Deprecated
        Key<T> getKey​(FindOptions options)
        Deprecated.
        use first() instead
        Get the key of the first entity in the result set. Obeys the Query offset value.
        Parameters:
        options - the options to apply to the find operation
        Returns:
        the key of the first instance in the result, or null if the result set is empty.
        Since:
        1.3
      • delete

        T delete()
      • modify

        Modify<T> modify​(UpdateOperations<T> operations)
        Deprecated.
        This is only intended for use as a bridge for the deprecated methods. Any use will break in the future.
        Parameters:
        operations - the prebuilt operations
        Returns:
        the Modify instance
        Since:
        2.0
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.
      • remove

        default com.mongodb.WriteResult remove()
      • remove

        com.mongodb.WriteResult remove​(DeleteOptions options)
      • update

        @Deprecated(since="2.0",
                    forRemoval=true)
        QueryImpl.Update update​(UpdateOperations operations)
        Deprecated, for removal: This API element is subject to removal in a future version.
        This is only intended for use as a bridge for the deprecated methods. Any use will break in the future.
        Parameters:
        operations - the prebuilt operations
        Returns:
        the Updates instance
        Since:
        2.0
        Developer note. This is an internal item. Its function and presence are subject to change without warning. Its use is highly discouraged.