Package com.mongodb.client.model
Class Projections
java.lang.Object
com.mongodb.client.model.Projections
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
Modifier and TypeMethodDescriptionstatic <TExpression>
BsonCreates a projection of a field whose value is computed from the given expression.static Bson
Creates a projection that includes for the given field only the first element of an array that matches the query filter.static Bson
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
Creates a projection that excludes all of the given fields.static Bson
Creates a projection that excludes all of the given fields.static Bson
Creates a projection that excludes the _id field.static Bson
Creates a projection that combines the list of projections into a single one.static Bson
Creates a projection that combines the list of projections into a single one.static Bson
Creates a projection that includes all of the given fields.static Bson
Creates a projection that includes all of the given fields.static Bson
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
Creates a projection to the given field name of a slice of the array value of that field.static Bson
Creates a projection to the given field name of a slice of the array value of that field.
-
Method Details
-
computed
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:
-
include
Creates a projection that includes all of the given fields.- Parameters:
fieldNames
- the field names- Returns:
- the projection
-
include
Creates a projection that includes all of the given fields.- Parameters:
fieldNames
- the field names- Returns:
- the projection
-
exclude
Creates a projection that excludes all of the given fields.- Parameters:
fieldNames
- the field names- Returns:
- the projection
-
exclude
Creates a projection that excludes all of the given fields.- Parameters:
fieldNames
- the field names- Returns:
- the projection
-
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
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
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
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
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
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
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
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
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
-