Class QueryImpl<T>

  • Type Parameters:
    T - The type we will be querying for, and returning.
    All Implemented Interfaces:
    com.mongodb.client.MongoIterable<T>, Criteria, CriteriaContainer, Query<T>, QueryResults<T>, java.lang.Iterable<T>

    public class QueryImpl<T>
    extends java.lang.Object
    implements CriteriaContainer, Query<T>
    Implementation of Query
    • Constructor Detail

      • QueryImpl

        public QueryImpl​(java.lang.Class<T> clazz,
                         com.mongodb.DBCollection coll,
                         Datastore ds)
        Creates a Query for the given type and collection
        Parameters:
        clazz - the type to return
        coll - the collection to query
        ds - the Datastore to use
    • Method Detail

      • parseFieldsString

        @Deprecated
        public static com.mongodb.BasicDBObject parseFieldsString​(java.lang.String str,
                                                                  java.lang.Class clazz,
                                                                  Mapper mapper,
                                                                  boolean validate)
        Deprecated.
        this is an internal method and will be removed in the next version
        Parses the string and validates each part
        Parameters:
        str - the String to parse
        clazz - the class to use when validating
        mapper - the Mapper to use
        validate - true if the results should be validated
        Returns:
        the DBObject
      • keys

        public MorphiaKeyCursor<T> keys()
        Description copied from interface: Query
        Execute the query and get the results (as a MorphiaCursor<Key<T>>)
        Specified by:
        keys in interface Query<T>
        Returns:
        the keys of the documents returned by this query
      • keys

        public MorphiaKeyCursor<T> keys​(FindOptions options)
        Description copied from interface: Query
        Execute the query and get the results (as a MorphiaCursor<Key<T>>)
        Specified by:
        keys in interface Query<T>
        Parameters:
        options - the options to apply to the find operation
        Returns:
        the keys of the documents returned by this query
      • asKeyList

        public java.util.List<Key<T>> asKeyList()
        Description copied from interface: Query
        Execute the query and get the results (as a List<Key<T>>) This method is provided as a convenience;
        Specified by:
        asKeyList in interface Query<T>
        Returns:
        returns a List of the keys of the documents returned by a query
      • asKeyList

        public java.util.List<Key<T>> asKeyList​(FindOptions options)
        Description copied from interface: Query
        Execute the query and get the results (as a List<Key<T>>) This method is provided as a convenience;
        Specified by:
        asKeyList in interface Query<T>
        Parameters:
        options - the options to apply to the find operation
        Returns:
        returns a List of the keys of the documents returned by a query
      • asList

        public java.util.List<T> asList()
        Description copied from interface: Query
        Execute the query and get the results.
        Specified by:
        asList in interface Query<T>
        Returns:
        returns a List of the documents returned by a query
      • asList

        public java.util.List<T> asList​(FindOptions options)
        Description copied from interface: Query
        Execute the query and get the results.
        Specified by:
        asList in interface Query<T>
        Parameters:
        options - the options to apply to the find operation
        Returns:
        returns a List of the documents returned by a query
      • countAll

        @Deprecated
        public long countAll()
        Deprecated.
        Description copied from interface: Query
        Count the total number of values in the result, ignoring limit and offset
        Specified by:
        countAll in interface Query<T>
        Returns:
        the count
      • count

        public long count()
        Description copied from interface: Query
        Count the total number of values in the result, ignoring limit and offset
        Specified by:
        count in interface Query<T>
        Returns:
        the count
      • count

        public long count​(CountOptions options)
        Description copied from interface: Query
        Count the total number of values in the result, ignoring limit and offset
        Specified by:
        count in interface Query<T>
        Parameters:
        options - the options to apply to the count operation
        Returns:
        the count
      • fetch

        public MorphiaIterator<T,​T> fetch()
        Description copied from interface: Query
        Execute the query and get the results.
        Specified by:
        fetch in interface Query<T>
        Returns:
        an Iterator of the results
      • fetch

        public MorphiaIterator<T,​T> fetch​(FindOptions options)
        Description copied from interface: Query
        Execute the query and get the results.
        Specified by:
        fetch in interface Query<T>
        Parameters:
        options - the options to apply to the find operation
        Returns:
        an Iterator of the results
      • find

        public MorphiaCursor<T> find()
        Description copied from interface: Query
        Execute the query and get the results. *note* the return type of this will change in 2.0.
        Specified by:
        find in interface Query<T>
        Returns:
        a MorphiaCursor
        See Also:
        Query.find(FindOptions)
      • find

        public MorphiaCursor<T> find​(FindOptions options)
        Description copied from interface: Query
        Execute the query and get the results.
        Specified by:
        find in interface Query<T>
        Parameters:
        options - the options to apply to the find operation
        Returns:
        a MorphiaCursor
      • fetchEmptyEntities

        public MorphiaIterator<T,​T> fetchEmptyEntities()
        Description copied from interface: Query
        Execute the query and get only the ids of the results. This is more efficient than fetching the actual results (transfers less data).
        Specified by:
        fetchEmptyEntities in interface Query<T>
        Returns:
        an Iterator of the empty entities
      • fetchEmptyEntities

        public MorphiaIterator<T,​T> fetchEmptyEntities​(FindOptions options)
        Description copied from interface: Query
        Execute the query and get only the ids of the results. This is more efficient than fetching the actual results (transfers less data).
        Specified by:
        fetchEmptyEntities in interface Query<T>
        Parameters:
        options - the options to apply to the find operation
        Returns:
        an Iterator of the empty entities
      • fetchKeys

        public MorphiaKeyIterator<T> fetchKeys​(FindOptions options)
        Description copied from interface: Query
        Execute the query and get the keys for the objects.
        Specified by:
        fetchKeys in interface Query<T>
        Parameters:
        options - the options to apply to the find operation
        Returns:
        the Key Iterator
      • first

        public T first()
        Specified by:
        first in interface com.mongodb.client.MongoIterable<T>
      • first

        public T first​(FindOptions options)
        Description copied from interface: Query
        Gets the first entity in the result set. Obeys the Query offset value.
        Specified by:
        first in interface Query<T>
        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.
      • get

        public T get()
        Description copied from interface: Query
        Gets the first entity in the result set. Obeys the Query offset value.
        Specified by:
        get in interface Query<T>
        Returns:
        the only instance in the result, or null if the result set is empty.
      • get

        public T get​(FindOptions options)
        Description copied from interface: Query
        Gets the first entity in the result set. Obeys the Query offset value.
        Specified by:
        get in interface Query<T>
        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.
      • getKey

        public Key<T> getKey()
        Description copied from interface: Query
        Get the key of the first entity in the result set. Obeys the Query offset value.
        Specified by:
        getKey in interface Query<T>
        Returns:
        the key of the first instance in the result, or null if the result set is empty.
      • getKey

        public Key<T> getKey​(FindOptions options)
        Description copied from interface: Query
        Get the key of the first entity in the result set. Obeys the Query offset value.
        Specified by:
        getKey in interface Query<T>
        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.
      • tail

        @Deprecated
        public MorphiaIterator<T,​T> tail​(boolean awaitData)
        Deprecated.
        Description copied from interface: Query
        Returns an tailing iterator over a set of elements of type T. If awaitData is true, this iterator blocks on hasNext() until new data is avail (or some amount of time has passed). Note that if no data is available at all, hasNext() might return immediately. You should wrap tail calls in a loop if you want this to be blocking.
        Specified by:
        tail in interface Query<T>
        Parameters:
        awaitData - passes the awaitData to the cursor
        Returns:
        an Iterator.
      • batchSize

        @Deprecated
        public Query<T> batchSize​(int value)
        Deprecated.
        Description copied from interface: Query
        Batch-size of the fetched result (cursor).
        Specified by:
        batchSize in interface com.mongodb.client.MongoIterable<T>
        Specified by:
        batchSize in interface Query<T>
        Parameters:
        value - must be >= 0. A value of 0 indicates the server default.
        Returns:
        this
        See Also:
        FindOptions.batchSize(int)
      • cloneQuery

        public QueryImpl<T> cloneQuery()
        Description copied from interface: Query
        Creates and returns a copy of this Query.
        Specified by:
        cloneQuery in interface Query<T>
        Returns:
        this
      • comment

        @Deprecated
        public Query<T> comment​(java.lang.String comment)
        Deprecated.
        Description copied from interface: Query
        This makes it possible to attach a comment to a query. Because these comments propagate to the profile log, adding comments can make your profile data much easier to interpret and trace.
        Specified by:
        comment in interface Query<T>
        Parameters:
        comment - the comment to add
        Returns:
        the Query to enable chaining of commands
      • disableValidation

        public Query<T> disableValidation()
        Description copied from interface: Query
        Turns off validation (for all calls made after)
        Specified by:
        disableValidation in interface Query<T>
        Returns:
        this
      • enableValidation

        public Query<T> enableValidation()
        Description copied from interface: Query
        Turns on validation (for all calls made after); by default validation is on
        Specified by:
        enableValidation in interface Query<T>
        Returns:
        this
      • explain

        public java.util.Map<java.lang.String,​java.lang.Object> explain()
        Description copied from interface: Query
        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.
        Specified by:
        explain in interface Query<T>
        Returns:
        Map describing the process used to return the query results.
      • explain

        public java.util.Map<java.lang.String,​java.lang.Object> explain​(FindOptions options)
        Description copied from interface: Query
        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.
        Specified by:
        explain in interface Query<T>
        Parameters:
        options - the options to apply to the explain operation
        Returns:
        Map describing the process used to return the query results.
      • field

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

        public Query<T> filter​(java.lang.String condition,
                               java.lang.Object value)
        Description copied from interface: Query
        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.

        Specified by:
        filter in interface Query<T>
        Parameters:
        condition - the condition to apply
        value - the value to apply against
        Returns:
        this
      • getCollection

        @Deprecated
        public com.mongodb.DBCollection getCollection()
        Deprecated.
        Specified by:
        getCollection in interface Query<T>
        Returns:
        the collection this query targets
      • getEntityClass

        public java.lang.Class<T> getEntityClass()
        Specified by:
        getEntityClass in interface Query<T>
        Returns:
        the entity Class.
      • getFieldsObject

        @Deprecated
        public com.mongodb.DBObject getFieldsObject()
        Deprecated.
        Specified by:
        getFieldsObject in interface Query<T>
        Returns:
        the Mongo fields DBObject.
      • getQueryObject

        @Deprecated
        public com.mongodb.DBObject getQueryObject()
        Deprecated.
        Specified by:
        getQueryObject in interface Query<T>
        Returns:
        the Mongo query DBObject.
      • setQueryObject

        public void setQueryObject​(com.mongodb.DBObject query)
        Sets query structure directly
        Parameters:
        query - the DBObject containing the query
      • getSortObject

        @Deprecated
        public com.mongodb.DBObject getSortObject()
        Deprecated.
        Specified by:
        getSortObject in interface Query<T>
        Returns:
        the Mongo sort DBObject.
      • hintIndex

        @Deprecated
        public Query<T> hintIndex​(java.lang.String idxName)
        Deprecated.
        Description copied from interface: Query
        Hints as to which index should be used.
        Specified by:
        hintIndex in interface Query<T>
        Parameters:
        idxName - the index name to hint
        Returns:
        this
        See Also:
        FindOptions.hint(DBObject)
      • limit

        @Deprecated
        public Query<T> limit​(int value)
        Deprecated.
        Description copied from interface: Query
        Limit the fetched result set to a certain number of values.
        Specified by:
        limit in interface Query<T>
        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
        public Query<T> lowerIndexBound​(com.mongodb.DBObject lowerBound)
        Deprecated.
        Description copied from interface: Query

        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"}

        Specified by:
        lowerIndexBound in interface Query<T>
        Parameters:
        lowerBound - The inclusive lower bound.
        Returns:
        this
        See Also:
        FindOptions.min(DBObject)
      • forEach

        public void forEach​(java.util.function.Consumer<? super T> block)
        Specified by:
        forEach in interface java.lang.Iterable<T>
      • maxTime

        @Deprecated
        public Query<T> maxTime​(long value,
                                java.util.concurrent.TimeUnit unit)
        Deprecated.
        Description copied from interface: Query
        Specifies a time limit for executing the query. Requires server version 2.6 or above.
        Specified by:
        maxTime in interface Query<T>
        Parameters:
        value - must be > 0. A value < 0 indicates no limit
        unit - the unit of time to use
        Returns:
        this
        See Also:
        FindOptions.maxTime(long, TimeUnit)
      • offset

        @Deprecated
        public Query<T> offset​(int value)
        Deprecated.
        Description copied from interface: Query
        Starts the query results at a particular zero-based offset.
        Specified by:
        offset in interface Query<T>
        Parameters:
        value - must be >= 0
        Returns:
        this
        See Also:
        FindOptions.skip(int)
      • order

        public Query<T> order​(java.lang.String sort)
        Description copied from interface: Query
        Sorts based on a property (defines return order). Examples:

        • order("age")
        • order("-age") (descending order)
        • order("age, date")
        • order("age,-date") (age ascending, date descending)
        Specified by:
        order in interface Query<T>
        Parameters:
        sort - the sort order to apply
        Returns:
        this
      • order

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

        public Query<T> order​(Sort... sorts)
        Description copied from interface: Query
        Sorts based on a specified sort keys (defines return order).
        Specified by:
        order in interface Query<T>
        Parameters:
        sorts - the sort order to apply
        Returns:
        this
      • queryNonPrimary

        @Deprecated
        public Query<T> queryNonPrimary()
        Deprecated.
        Description copied from interface: Query
        Route query to non-primary node
        Specified by:
        queryNonPrimary in interface Query<T>
        Returns:
        this
        See Also:
        ReadPreference.secondary(), ReadPreference.secondaryPreferred(), FindOptions.readPreference(ReadPreference), ReadPreference.secondary(), ReadPreference.secondaryPreferred()
      • retrieveKnownFields

        public Query<T> retrieveKnownFields()
        Description copied from interface: Query
        Limits the fields retrieved to those of the query type -- dangerous with interfaces and abstract classes
        Specified by:
        retrieveKnownFields in interface Query<T>
        Returns:
        this
      • project

        public Query<T> project​(java.lang.String field,
                                boolean include)
        Description copied from interface: Query
        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.
        Specified by:
        project in interface Query<T>
        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
      • project

        public Query<T> project​(java.lang.String field,
                                ArraySlice slice)
        Description copied from interface: Query
        Adds an sliced array field to a projection.
        Specified by:
        project in interface Query<T>
        Parameters:
        field - the field to project
        slice - the options for projecting an array field
        Returns:
        this
        See Also:
        Project Fields to Return from Query
      • retrievedFields

        @Deprecated
        public Query<T> retrievedFields​(boolean include,
                                        java.lang.String... list)
        Deprecated.
        Description copied from interface: Query
        Limits the fields retrieved
        Specified by:
        retrievedFields in interface Query<T>
        Parameters:
        include - true if the fields should be included in the results. false to exclude them.
        list - the fields in question
        Returns:
        this
      • into

        public <A extends java.util.Collection<? super T>> A into​(A target)
        Specified by:
        into in interface com.mongodb.client.MongoIterable<T>
      • search

        public Query<T> search​(java.lang.String search)
        Description copied from interface: Query
        Perform a text search on the content of the fields indexed with a text index..
        Specified by:
        search in interface Query<T>
        Parameters:
        search - the text to search for
        Returns:
        the Query to enable chaining of commands
      • search

        public Query<T> search​(java.lang.String search,
                               java.lang.String language)
        Description copied from interface: Query
        Perform a text search on the content of the fields indexed with a text index..
        Specified by:
        search in interface Query<T>
        Parameters:
        search - the text to search for
        language - the language to use during the search
        Returns:
        the Query to enable chaining of commands
      • maxScan

        public Query<T> maxScan​(int value)
        Description copied from interface: Query
        Constrains the query to only scan the specified number of documents when fulfilling the query.
        Specified by:
        maxScan in interface Query<T>
        Parameters:
        value - must be > 0. A value < 0 indicates no limit
        Returns:
        this
      • useReadPreference

        @Deprecated
        public Query<T> useReadPreference​(com.mongodb.ReadPreference readPref)
        Deprecated.
        Description copied from interface: Query
        Updates the ReadPreference to use
        Specified by:
        useReadPreference in interface Query<T>
        Parameters:
        readPref - the ReadPreference to use
        Returns:
        this
        See Also:
        ReadPreference, FindOptions.readPreference(ReadPreference)
      • where

        public Query<T> where​(java.lang.String js)
        Description copied from interface: Query
        Limit the query using this javascript block; only one per query
        Specified by:
        where in interface Query<T>
        Parameters:
        js - the javascript block to apply
        Returns:
        this
      • where

        public Query<T> where​(org.bson.types.CodeWScope js)
        Description copied from interface: Query
        Limit the query using this javascript block; only one per query
        Specified by:
        where in interface Query<T>
        Parameters:
        js - the javascript block to apply
        Returns:
        this
      • getFieldName

        public java.lang.String getFieldName()
        Specified by:
        getFieldName in interface Criteria
        Returns:
        the field name for the criteria
      • getDatastore

        @Deprecated
        public DatastoreImpl getDatastore()
        Deprecated.
        this is an internal method that exposes an internal type and will likely go away soon
        Returns:
        the Datastore
      • isValidatingNames

        public boolean isValidatingNames()
        Returns:
        true if field names are being validated
      • isValidatingTypes

        public boolean isValidatingTypes()
        Returns:
        true if query parameter value types are being validated against the field types
      • iterator

        public com.mongodb.client.MongoCursor<T> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface com.mongodb.client.MongoIterable<T>
      • cursor

        public com.mongodb.client.MongoCursor<T> cursor()
        Specified by:
        cursor in interface com.mongodb.client.MongoIterable<T>
      • prepareCursor

        @Deprecated
        public com.mongodb.DBCursor prepareCursor()
        Deprecated.
        this is an internal method. no replacement is planned.
        Prepares cursor for iteration
        Returns:
        the cursor
      • map

        public <U> com.mongodb.client.MongoIterable<U> map​(com.mongodb.Function<T,​U> mapper)
        Specified by:
        map in interface com.mongodb.client.MongoIterable<T>
      • returnKey

        @Deprecated
        public Query<T> returnKey()
        Deprecated.
        Description copied from interface: Query
        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
        Specified by:
        returnKey in interface Query<T>
        Returns:
        the Query to enable chaining of commands
        See Also:
        FindOptions.returnKey(boolean)
      • upperIndexBound

        @Deprecated
        public Query<T> upperIndexBound​(com.mongodb.DBObject upperBound)
        Deprecated.
        Description copied from interface: Query

        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"}

        Specified by:
        upperIndexBound in interface Query<T>
        Parameters:
        upperBound - The exclusive upper bound.
        Returns:
        this
        See Also:
        FindOptions.max(DBObject)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • add

        public void add​(Criteria... criteria)
        Description copied from interface: CriteriaContainer
        Adds Criteria to this container
        Specified by:
        add in interface CriteriaContainer
        Parameters:
        criteria - the criteria to add
      • toDBObject

        public com.mongodb.DBObject toDBObject()
        Specified by:
        toDBObject in interface Criteria
        Returns:
        the DBObject form of this type
      • remove

        public void remove​(Criteria criteria)
        Description copied from interface: CriteriaContainer
        Removes Criteria to this container
        Specified by:
        remove in interface CriteriaContainer
        Parameters:
        criteria - the criteria to remove
      • attach

        public void attach​(CriteriaContainer container)
        Description copied from interface: Criteria
        Used to add this Criteria to a CriteriaContainer
        Specified by:
        attach in interface Criteria
        Parameters:
        container - the container to add to