All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mongodb.morphia.query.Query Maven / Gradle / Ivy

The newest version!
package org.mongodb.morphia.query;


import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.ReadPreference;
import org.bson.types.CodeWScope;

import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @param  The java type to query against
 * @author Scott Hernandez
 */
public interface Query extends QueryResults, Cloneable {
    /**
     * Creates a container to hold 'and' clauses
     *
     * @param criteria the clauses to 'and' together
     * @return the container
     */
    CriteriaContainer and(Criteria... criteria);

    /**
     * Batch-size of the fetched result (cursor).
     *
     * @param value must be >= 0.  A value of 0 indicates the server default.
     * @return this
     * @deprecated use the methods that accept Options directly
     * @see FindOptions#batchSize(int)
     */
    @Deprecated
    Query batchSize(int value);

    /**
     * Creates and returns a copy of this {@link Query}.
     *
     * @return this
     */
    Query cloneQuery();

    /**
     * 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.
     *
     * @param comment the comment to add
     * @return the Query to enable chaining of commands
     * @mongodb.driver.manual reference/operator/meta/comment $comment
     * @see FindOptions#modifier(String, Object)
     * @deprecated use the methods that accept Options directly. This can be replicated with {@code options.modifier("$comment", comment)}
     */
    @Deprecated
    Query comment(String comment);

    /**
     * Creates a criteria to apply against a field
     *
     * @param field the field
     * @return the FieldEnd to define the criteria
     */
    FieldEnd criteria(String field);

    /**
     * Disables cursor timeout on server.
     *
     * @return this
     * @deprecated use the methods that accept Options directly
     * @see FindOptions#noCursorTimeout(boolean)
     */
    @Deprecated
    Query disableCursorTimeout();

    /**
     * Disable snapshotted mode (default mode). This will be faster but changes made during the cursor may cause duplicates.
     *
     * @return this
     * @deprecated use the methods that accept Options directly.  This can be replicated using {@code options.modifier("$snapshot", false)}
     * @see FindOptions#modifier(String, Object)
     */
    @Deprecated
    Query disableSnapshotMode();

    /**
     * Turns off validation (for all calls made after)
     *
     * @return this
     */
    Query disableValidation();

    /**
     * Enables cursor timeout on server.
     *
     * @return this
     * @deprecated use the methods that accept Options directly
     * @see FindOptions#noCursorTimeout(boolean)
     */
    @Deprecated
    Query enableCursorTimeout();

    /**
     * Enables snapshotted mode where duplicate results (which may be updated during the lifetime of the cursor) will not be returned. Not
     * compatible with order/sort and hint.
     *
     * @return this
     * @deprecated use the methods that accept Options directly.  This can be replicated using {@code options.modifier("$snapshot", true)}
     * @see FindOptions#modifier(String, Object)
     */
    @Deprecated
    Query enableSnapshotMode();

    /**
     * Turns on validation (for all calls made after); by default validation is on
     *
     * @return this
     */
    Query enableValidation();

    /**
     * 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.
     *
     * @return Map describing the process used to return the query results.
     * @mongodb.driver.manual reference/operator/meta/explain/ explain
     */
    Map 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.
     *
     * @param options the options to apply to the explain operation
     * @return Map describing the process used to return the query results.
     * @mongodb.driver.manual reference/operator/meta/explain/ explain
     * @since 1.3
     */
    Map explain(FindOptions options);

    /**
     * Fluent query interface: {@code createQuery(Ent.class).field("count").greaterThan(7)...}
     *
     * @param field the field
     * @return the FieldEnd to define the criteria
     */
    FieldEnd> field(String field);

    /**
     * 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:

*

*

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

*

You can filter on id properties if this query is restricted to a Class. * * @param condition the condition to apply * @param value the value to apply against * @return this */ Query filter(String condition, Object value); /** * @return the batch size * @see #batchSize(int) * @deprecated use the methods that accept Options directly * @see FindOptions#batchSize(int) */ @Deprecated int getBatchSize(); /** * @return the {@link DBCollection} of the {@link Query}. * * @deprecated This is an internal method and subject to change or removal. Do not use. */ @Deprecated DBCollection getCollection(); /** * @return the entity {@link Class}. */ Class getEntityClass(); /** * @return the Mongo fields {@link DBObject}. * @deprecated This is an internal method and subject to change or removal. Do not use. */ @Deprecated DBObject getFieldsObject(); /** * @return the limit * @see #limit(int) * @deprecated use the methods that accept Options directly * @see FindOptions#limit(int) */ @Deprecated int getLimit(); /** * @return the offset. * @see #offset(int) * @deprecated use the methods that accept Options directly * @see FindOptions#getSkip() */ @Deprecated int getOffset(); /** * @return the Mongo query {@link DBObject}. * @deprecated This is an internal method and subject to change or removal. Do not use. */ @Deprecated DBObject getQueryObject(); /** * @return the Mongo sort {@link DBObject}. * @deprecated This is an internal method and subject to change or removal. Do not use. */ @Deprecated DBObject getSortObject(); /** * Hints as to which index should be used. * * @param idxName the index name to hint * @return this * @deprecated use the methods that accept Options directly. This can be replicated with {@code options.modifier("$hint", idxName)} * @see FindOptions#modifier(String, Object) */ @Deprecated Query hintIndex(String idxName); /** * Limit the fetched result set to a certain number of values. * * @param value must be >= 0. A value of 0 indicates no limit. For values < 0, use {@link FindOptions#batchSize(int)} which * is the preferred method * @return this * @deprecated use the methods that accept Options directly * @see FindOptions#limit(int) */ @Deprecated Query limit(int value); /** *

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:

{@code query.lowerIndexBound(new * BasicDBObject("a", 1).append("b", 2)); }

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

* * @param lowerBound The inclusive lower bound. * @return this * @mongodb.driver.manual reference/operator/meta/min/ $min * @deprecated use the methods that accept Options directly. This can be replicated using * {@code options.modifier("$min", new Document(...)) } * @see FindOptions#modifier(String, Object) */ @Deprecated Query lowerIndexBound(DBObject lowerBound); /** * Constrains the query to only scan the specified number of documents when fulfilling the query. * * @param value must be > 0. A value < 0 indicates no limit * @return this * @mongodb.driver.manual reference/operator/meta/maxScan/#op._S_maxScan $maxScan * @deprecated use the methods that accept Options directly. This can be replicated using {@code options.modifier("$maxScan", value) } * @see FindOptions#modifier(String, Object) */ @Deprecated Query maxScan(int value); /** * Specifies a time limit for executing the query. Requires server version 2.6 or above. * * @param maxTime must be > 0. A value < 0 indicates no limit * @param maxTimeUnit the unit of time to use * @return this * @deprecated use the methods that accept Options directly. This can be replicated using {@code options.modifier("$maxTimeMS", * MILLISECONDS.convert(value, unit)) } * @see FindOptions#modifier(String, Object) */ @Deprecated Query maxTime(long maxTime, TimeUnit maxTimeUnit); /** * Starts the query results at a particular zero-based offset. * * @param value must be >= 0 * @return this * @deprecated use the methods that accept Options directly * @see FindOptions#skip(int) */ @Deprecated Query offset(int value); /** * Creates a container to hold 'or' clauses * * @param criteria the clauses to 'or' together * @return the container */ CriteriaContainer or(Criteria... criteria); /** * Sorts based on a property (defines return order). Examples: *

*

    *
  • {@code order("age")}
  • *
  • {@code order("-age")} (descending order)
  • *
  • {@code order("age, date")}
  • *
  • {@code order("age,-date")} (age ascending, date descending)
  • *
* * @param sort the sort order to apply * @return this */ Query order(String sort); /** * Sorts based on a metadata (defines return order). Example: * {@code order(Meta.textScore())} ({textScore : { $meta: "textScore" }}) * @param sort the sort order to apply * @return this */ Query order(Meta sort); /** * Sorts based on a specified sort keys (defines return order). * * @param sorts the sort order to apply * @return this */ Query order(Sort... sorts); /** * 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. * * @param field the field to project * @param include true to include the field in the results * @return this * @see Project Fields to Return from Query */ Query project(String field, boolean include); /** * Adds an sliced array field to a projection. * * @param field the field to project * @param slice the options for projecting an array field * @return this * @see Project Fields to Return from Query * @mongodb.driver.manual /reference/operator/projection/slice/ $slice */ Query project(String field, ArraySlice slice); /** * Adds a metadata field to a projection. * * @param meta the metadata option for projecting * @return this * @see Project Fields to Return from Query * @mongodb.driver.manual reference/operator/projection/meta/ $meta */ Query project(Meta meta); /** * Route query to non-primary node * * @return this * @see ReadPreference#secondary() * @see ReadPreference#secondaryPreferred() * @deprecated use the methods that accept Options directly * @see FindOptions#readPreference(ReadPreference) * @see ReadPreference#secondary() * @see ReadPreference#secondaryPreferred() */ @Deprecated Query queryNonPrimary(); /** * Route query to primary node * * @return this * @see ReadPreference#primary() * @deprecated use the methods that accept Options directly. * @see FindOptions#readPreference(ReadPreference) * @see ReadPreference#primary() * @see ReadPreference#primaryPreferred() */ @Deprecated Query queryPrimaryOnly(); /** * Limits the fields retrieved to those of the query type -- dangerous with interfaces and abstract classes * * @return this */ Query retrieveKnownFields(); /** * Limits the fields retrieved * * @param include true if the fields should be included in the results. false to exclude them. * @param fields the fields in question * @return this * @deprecated use {@link #project(String, boolean)} instead */ @Deprecated Query retrievedFields(boolean include, String... fields); /** * 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 * * @return the Query to enable chaining of commands * @mongodb.driver.manual reference/operator/meta/returnKey/#op._S_returnKey $returnKey * @deprecated use the methods that accept Options directly. This can be replicated using {@code options.modifier("$returnKey", true) } * @see FindOptions#modifier(String, Object) */ @Deprecated Query returnKey(); /** * Perform a text search on the content of the fields indexed with a text index.. * * @param text the text to search for * @return the Query to enable chaining of commands * @mongodb.driver.manual reference/operator/query/text/ $text */ Query search(String text); /** * Perform a text search on the content of the fields indexed with a text index.. * * @param text the text to search for * @param language the language to use during the search * @return the Query to enable chaining of commands * @mongodb.driver.manual reference/operator/query/text/ $text */ Query search(String text, String language); /** *

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:

{@code query.upperIndexBound(new * BasicDBObject("a", 1).append("b", 2)); }

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

* * @param upperBound The exclusive upper bound. * @return this * @mongodb.driver.manual reference/operator/meta/max/ $max * @deprecated use the methods that accept Options directly. This can be replicated using * {@code options.modifier("$max", new Document(...)) } * @see FindOptions#modifier(String, Object) */ @Deprecated Query upperIndexBound(DBObject upperBound); /** * Updates the ReadPreference to use * * @param readPref the ReadPreference to use * @return this * @see ReadPreference * @deprecated use the methods that accept Options directly * @see FindOptions#readPreference(ReadPreference) */ @Deprecated Query useReadPreference(ReadPreference readPref); /** * Limit the query using this javascript block; only one per query * * @param js the javascript block to apply * @return this */ Query where(String js); /** * Limit the query using this javascript block; only one per query * * @param js the javascript block to apply * @return this */ Query where(CodeWScope js); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy