java.lang.Object
org.springframework.data.mongodb.core.query.Query
All Implemented Interfaces:
ReadConcernAware, ReadPreferenceAware
Direct Known Subclasses:
BasicQuery, TextQuery

public class Query extends Object implements ReadConcernAware, ReadPreferenceAware
MongoDB Query object representing criteria, projection, sorting and query hints.
Author:
Thomas Risberg, Oliver Gierke, Thomas Darimont, Christoph Strobl, Mark Paluch, Anton Barkan
  • Constructor Details

  • Method Details

    • query

      public static Query query(CriteriaDefinition criteriaDefinition)
      Static factory method to create a Query using the provided CriteriaDefinition.
      Parameters:
      criteriaDefinition - must not be null.
      Returns:
      new instance of Query.
      Since:
      1.6
    • addCriteria

      public Query addCriteria(CriteriaDefinition criteriaDefinition)
      Adds the given CriteriaDefinition to the current Query.
      Parameters:
      criteriaDefinition - must not be null.
      Returns:
      this.
      Since:
      1.6
    • fields

      public Field fields()
    • skip

      public Query skip(long skip)
      Set number of documents to skip before returning results. Use zero or a negative value to avoid skipping.
      Parameters:
      skip - number of documents to skip. Use zero or a negative value to avoid skipping.
      Returns:
      this.
    • limit

      public Query limit(int limit)
      Limit the number of returned documents to limit. A zero or negative value is considered as unlimited.
      Parameters:
      limit - number of documents to return. Use zero or negative for unlimited.
      Returns:
      this.
    • limit

      public Query limit(Limit limit)
      Limit the number of returned documents to Limit.
      Parameters:
      limit - number of documents to return.
      Returns:
      this.
      Since:
      4.2
    • withHint

      public Query withHint(String hint)
      Configures the query to use the given hint when being executed. The hint can either be an index name or a json Document representation.
      Parameters:
      hint - must not be null or empty.
      Returns:
      this.
      See Also:
      • Document.parse(String)
    • withReadConcern

      public Query withReadConcern(com.mongodb.ReadConcern readConcern)
      Configures the query to use the given ReadConcern when being executed.
      Parameters:
      readConcern - must not be null.
      Returns:
      this.
      Since:
      3.1
    • withReadPreference

      public Query withReadPreference(com.mongodb.ReadPreference readPreference)
      Configures the query to use the given ReadPreference when being executed.
      Parameters:
      readPreference - must not be null.
      Returns:
      this.
      Since:
      4.1
    • hasReadConcern

      public boolean hasReadConcern()
      Specified by:
      hasReadConcern in interface ReadConcernAware
      Returns:
      true if a ReadConcern is set.
    • getReadConcern

      public com.mongodb.ReadConcern getReadConcern()
      Specified by:
      getReadConcern in interface ReadConcernAware
      Returns:
      the ReadConcern to apply or null if none set.
    • hasReadPreference

      public boolean hasReadPreference()
      Specified by:
      hasReadPreference in interface ReadPreferenceAware
      Returns:
      true if a ReadPreference is set.
    • getReadPreference

      public com.mongodb.ReadPreference getReadPreference()
      Specified by:
      getReadPreference in interface ReadPreferenceAware
      Returns:
      the ReadPreference to apply or null if none set.
    • withHint

      public Query withHint(org.bson.Document hint)
      Configures the query to use the given hint when being executed.
      Parameters:
      hint - must not be null.
      Returns:
      this.
      Since:
      2.2
    • with

      public Query with(Pageable pageable)
      Sets the given pagination information on the Query instance. Will transparently set skip and limit as well as applying the Sort instance defined with the Pageable.
      Parameters:
      pageable - must not be null.
      Returns:
      this.
    • with

      public Query with(ScrollPosition position)
      Sets the given cursor position on the Query instance. Will transparently set skip.
      Parameters:
      position - must not be null.
      Returns:
      this.
    • with

      public Query with(OffsetScrollPosition position)
      Sets the given cursor position on the Query instance. Will transparently set skip.
      Parameters:
      position - must not be null.
      Returns:
      this.
    • with

      public Query with(KeysetScrollPosition position)
      Sets the given cursor position on the Query instance. Will transparently reset skip.
      Parameters:
      position - must not be null.
      Returns:
      this.
    • hasKeyset

      public boolean hasKeyset()
    • getKeyset

      @Nullable public KeysetScrollPosition getKeyset()
    • with

      public Query with(Sort sort)
      Adds a Sort to the Query instance.
      Parameters:
      sort - must not be null.
      Returns:
      this.
    • getRestrictedTypes

      public Set<Class<?>> getRestrictedTypes()
      Returns:
      the restrictedTypes
    • restrict

      public Query restrict(Class<?> type, Class<?>... additionalTypes)
      Restricts the query to only return documents instances that are exactly of the given types.
      Parameters:
      type - may not be null
      additionalTypes - may not be null
      Returns:
      this.
    • getQueryObject

      public org.bson.Document getQueryObject()
      Returns:
      the query Document.
    • getFieldsObject

      public org.bson.Document getFieldsObject()
      Returns:
      the field Document.
    • getSortObject

      public org.bson.Document getSortObject()
      Returns:
      the sort Document.
    • isSorted

      public boolean isSorted()
      Returns true if the Query has a sort parameter.
      Returns:
      true if sorted.
      Since:
      2.2
      See Also:
    • getSkip

      public long getSkip()
      Get the number of documents to skip. Zero or a negative value indicates no skip.
      Returns:
      number of documents to skip
    • isLimited

      public boolean isLimited()
      Returns whether the query is limited.
      Returns:
      true if the query is limited; false otherwise.
      Since:
      4.1
    • getLimit

      public int getLimit()
      Get the maximum number of documents to be return. Zero or a negative value indicates no limit.
      Returns:
      number of documents to return.
      See Also:
    • getHint

      @Nullable public String getHint()
      Returns:
      can be null.
    • maxTimeMsec

      public Query maxTimeMsec(long maxTimeMsec)
      Parameters:
      maxTimeMsec -
      Returns:
      this.
      Since:
      1.6
      See Also:
    • maxTime

      public Query maxTime(Duration timeout)
      Parameters:
      timeout - must not be null.
      Returns:
      this.
      Since:
      2.1
      See Also:
    • comment

      public Query comment(String comment)
      Add a comment to the query that is propagated to the profile log.
      Parameters:
      comment - must not be null.
      Returns:
      this.
      Since:
      1.6
      See Also:
    • allowDiskUse

      public Query allowDiskUse(boolean allowDiskUse)
      Enables writing to temporary files for aggregation stages and queries. When set to true, aggregation stages can write data to the _tmp subdirectory in the dbPath directory.

      Starting in MongoDB 4.2, the profiler log messages and diagnostic log messages includes a usedDisk indicator if any aggregation stage wrote data to temporary files due to memory restrictions.

      Parameters:
      allowDiskUse -
      Returns:
      this.
      Since:
      3.2
      See Also:
    • cursorBatchSize

      public Query cursorBatchSize(int batchSize)
      Set the number of documents to return in each response batch.
      Use 0 (zero) for no limit. A negative limit closes the cursor after returning a single batch indicating to the server that the client will not ask for a subsequent one.
      Parameters:
      batchSize - The number of documents to return per batch.
      Returns:
      this.
      Since:
      2.1
      See Also:
    • noCursorTimeout

      public Query noCursorTimeout()
      Returns:
      this.
      Since:
      1.10
      See Also:
    • exhaust

      public Query exhaust()
      Returns:
      this.
      Since:
      1.10
      See Also:
    • allowSecondaryReads

      public Query allowSecondaryReads()
      Allows querying of a replica.
      Returns:
      this.
      Since:
      3.0.2
      See Also:
    • partialResults

      public Query partialResults()
      Returns:
      this.
      Since:
      1.10
      See Also:
    • getMeta

      public Meta getMeta()
      Returns:
      never null.
      Since:
      1.6
    • setMeta

      public void setMeta(Meta meta)
      Parameters:
      meta - must not be null.
      Since:
      1.6
    • collation

      public Query collation(@Nullable Collation collation)
      Set the Collation applying language-specific rules for string comparison.
      Parameters:
      collation - can be null.
      Returns:
      this.
      Since:
      2.0
    • getCollation

      public Optional<Collation> getCollation()
      Get the Collation defining language-specific rules for string comparison.
      Returns:
      never null.
      Since:
      2.0
    • getCriteria

      protected List<CriteriaDefinition> getCriteria()
    • of

      public static Query of(Query source)
      Create an independent copy of the given Query.
      The resulting Query will not be binary equal to the given source but semantically equal in terms of creating the same result when executed.
      Parameters:
      source - The source Query to use a reference. Must not be null.
      Returns:
      new Query.
      Since:
      2.2
    • toString

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

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • querySettingsEquals

      protected boolean querySettingsEquals(Query that)
      Tests whether the settings of the given Query are equal to this query.
      Parameters:
      that -
      Returns:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • isRestrictedTypeKey

      @Deprecated public static boolean isRestrictedTypeKey(String key)
      Deprecated.
      don't call this method as the restricted type handling will undergo some significant changes going forward.
      Returns whether the given key is the one used to hold the type restriction information.
      Parameters:
      key -
      Returns: