Package com.mongodb.client.model
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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <TExpression>
Bsoncomputed(String fieldName, TExpression expression)
Creates a projection of a field whose value is computed from the given expression.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.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.static Bson
exclude(String... fieldNames)
Creates a projection that excludes all of the given fields.static Bson
exclude(List<String> fieldNames)
Creates a projection that excludes all of the given fields.static Bson
excludeId()
Creates a projection that excludes the _id field.static Bson
fields(List<? extends Bson> projections)
Creates a projection that combines the list of projections into a single one.static Bson
fields(Bson... projections)
Creates a projection that combines the list of projections into a single one.static Bson
include(String... fieldNames)
Creates a projection that includes all of the given fields.static Bson
include(List<String> fieldNames)
Creates a projection that includes all of the given fields.static Bson
meta(String fieldName, String metaFieldName)
Creates a $meta projection to the given field name for the given meta field name.static Bson
metaTextScore(String fieldName)
Creates a projection to the given field name of the textScore, for use with text queries.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.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.
-
-
-
Method Detail
-
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 nameexpression
- the expression- Returns:
- the projection
- See Also:
Aggregates.project(Bson)
-
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 namefilter
- 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 namemetaFieldName
- the meta field name- Returns:
- the projection
- Since:
- 4.1
- MongoDB documentation
- reference/operator/projection/meta/#projection
-
metaTextScore
public static Bson metaTextScore(String fieldName)
Creates a projection to the given field name of the textScore, for use with text queries.- Parameters:
fieldName
- the field name- Returns:
- the projection
- MongoDB documentation
- textScore
-
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 namelimit
- 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 nameskip
- the number of elements to skip before applying the limitlimit
- 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
-
-