Class Projections

java.lang.Object
com.mongodb.client.model.Projections

public final class Projections extends Object
A factory for projections. A convenient way to use this class is to statically import all of its methods, which allows usage like:
    collection.find().projection(fields(include("x", "y"), excludeId()))
 
Since:
3.0
MongoDB documentation
Projection
  • Method Details

    • computed

      public static <TExpression> Bson computed(String fieldName, TExpression expression)
      Creates a projection of a field whose value is computed from the given expression. Projection with an expression is only supported using the $project aggregation pipeline stage.
      Type Parameters:
      TExpression - the expression type
      Parameters:
      fieldName - the field name
      expression - the expression
      Returns:
      the projection
      See Also:
    • computedSearchMeta

      public static Bson computedSearchMeta(String fieldName)
      Creates a projection of a field whose value is equal to the $$SEARCH_META variable, for use with Aggregates.search(SearchOperator, SearchOptions) / Aggregates.search(SearchCollector, SearchOptions). Calling this method is equivalent to calling computed(String, Object) with "$$SEARCH_META" as the second argument.
      Parameters:
      fieldName - the field name
      Returns:
      the projection
      Since:
      4.7
      See Also:
    • include

      public static Bson include(String... fieldNames)
      Creates a projection that includes all of the given fields.
      Parameters:
      fieldNames - the field names
      Returns:
      the projection
    • include

      public static Bson include(List<String> fieldNames)
      Creates a projection that includes all of the given fields.
      Parameters:
      fieldNames - the field names
      Returns:
      the projection
    • exclude

      public static Bson exclude(String... fieldNames)
      Creates a projection that excludes all of the given fields.
      Parameters:
      fieldNames - the field names
      Returns:
      the projection
    • exclude

      public static Bson exclude(List<String> fieldNames)
      Creates a projection that excludes all of the given fields.
      Parameters:
      fieldNames - the field names
      Returns:
      the projection
    • excludeId

      public static Bson excludeId()
      Creates a projection that excludes the _id field. This suppresses the automatic inclusion of _id that is the default, even when other fields are explicitly included.
      Returns:
      the projection
    • elemMatch

      public static Bson elemMatch(String fieldName)
      Creates a projection that includes for the given field only the first element of an array that matches the query filter. This is referred to as the positional $ operator.
      Parameters:
      fieldName - the field name whose value is the array
      Returns:
      the projection
      MongoDB documentation
      Project the first matching element ($ operator)
    • elemMatch

      public static Bson elemMatch(String fieldName, Bson filter)
      Creates a projection that includes for the given field only the first element of the array value of that field that matches the given query filter.
      Parameters:
      fieldName - the field name
      filter - the filter to apply
      Returns:
      the projection
      MongoDB documentation
      elemMatch
    • meta

      public static Bson meta(String fieldName, String metaFieldName)
      Creates a $meta projection to the given field name for the given meta field name.
      Parameters:
      fieldName - the field name
      metaFieldName - the meta field name
      Returns:
      the projection
      Since:
      4.1
      See Also:
      MongoDB documentation
      reference/operator/aggregation/meta/
    • metaTextScore

      public static Bson metaTextScore(String fieldName)
      Creates a projection to the given field name of the textScore, for use with text queries. Calling this method is equivalent to calling meta(String, String) with "textScore" as the second argument.
      Parameters:
      fieldName - the field name
      Returns:
      the projection
      See Also:
      MongoDB documentation
      textScore
    • metaSearchScore

      public static Bson metaSearchScore(String fieldName)
      Creates a projection to the given field name of the searchScore, for use with Aggregates.search(SearchOperator, SearchOptions) / Aggregates.search(SearchCollector, SearchOptions). Calling this method is equivalent to calling meta(String, String) with "searchScore" as the second argument.
      Parameters:
      fieldName - the field name
      Returns:
      the projection
      Since:
      4.7
      MongoDB Atlas documentation
      Scoring
    • metaSearchHighlights

      public static Bson metaSearchHighlights(String fieldName)
      Creates a projection to the given field name of the searchHighlights, for use with Aggregates.search(SearchOperator, SearchOptions) / Aggregates.search(SearchCollector, SearchOptions). Calling this method is equivalent to calling meta(String, String) with "searchHighlights" as the second argument.
      Parameters:
      fieldName - the field name
      Returns:
      the projection
      Since:
      4.7
      See Also:
      MongoDB Atlas documentation
      Highlighting
    • slice

      public static Bson slice(String fieldName, int limit)
      Creates a projection to the given field name of a slice of the array value of that field.
      Parameters:
      fieldName - the field name
      limit - the number of elements to project.
      Returns:
      the projection
      MongoDB documentation
      Slice
    • slice

      public static Bson slice(String fieldName, int skip, int limit)
      Creates a projection to the given field name of a slice of the array value of that field.
      Parameters:
      fieldName - the field name
      skip - the number of elements to skip before applying the limit
      limit - the number of elements to project
      Returns:
      the projection
      MongoDB documentation
      Slice
    • fields

      public static Bson fields(Bson... projections)
      Creates a projection that combines the list of projections into a single one. If there are duplicate keys, the last one takes precedence.
      Parameters:
      projections - the list of projections to combine
      Returns:
      the combined projection
    • fields

      public static Bson fields(List<? extends Bson> projections)
      Creates a projection that combines the list of projections into a single one. If there are duplicate keys, the last one takes precedence.
      Parameters:
      projections - the list of projections to combine
      Returns:
      the combined projection