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
computedSearchMeta
(String fieldName) Creates a projection of a field whose value is equal to the$$SEARCH_META
variable, for use withAggregates.search(SearchOperator, SearchOptions)
/Aggregates.search(SearchCollector, SearchOptions)
.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
metaSearchHighlights
(String fieldName) Creates a projection to the given field name of the searchHighlights, for use withAggregates.search(SearchOperator, SearchOptions)
/Aggregates.search(SearchCollector, SearchOptions)
.static Bson
metaSearchScore
(String fieldName) Creates a projection to the given field name of the searchScore, for use withAggregates.search(SearchOperator, SearchOptions)
/Aggregates.search(SearchCollector, SearchOptions)
.static Bson
metaTextScore
(String fieldName) Creates a projection to the given field name of the textScore, for use with text queries.static Bson
metaVectorSearchScore
(String fieldName) Creates a projection to the given field name of the vectorSearchScore, for use withAggregates.vectorSearch(FieldSearchPath, Iterable, String, long, VectorSearchOptions)
.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 can be used in the following contexts:- $project aggregation pipeline stage.
- Starting from MongoDB 4.4, it's also accepted in various find-related methods within the
MongoCollection
-based API where projection is supported, for example:find()
findOneAndReplace()
findOneAndUpdate()
findOneAndDelete()
- Type Parameters:
TExpression
- the expression type- Parameters:
fieldName
- the field nameexpression
- the expression- Returns:
- the projection
- See Also:
-
computedSearchMeta
Creates a projection of a field whose value is equal to the$$SEARCH_META
variable, for use withAggregates.search(SearchOperator, SearchOptions)
/Aggregates.search(SearchCollector, SearchOptions)
. Calling this method is equivalent to callingcomputed(String, Object)
with"$$SEARCH_META"
as the second argument.- Parameters:
fieldName
- the field name- Returns:
- the projection
- Since:
- 4.7
- 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
- See Also:
- MongoDB documentation
- reference/operator/aggregation/meta/
-
metaTextScore
Creates a projection to the given field name of the textScore, for use with text queries. Calling this method is equivalent to callingmeta(String, String)
with"textScore"
as the second argument.- Parameters:
fieldName
- the field name- Returns:
- the projection
- See Also:
- MongoDB documentation
- textScore
-
metaSearchScore
Creates a projection to the given field name of the searchScore, for use withAggregates.search(SearchOperator, SearchOptions)
/Aggregates.search(SearchCollector, SearchOptions)
. Calling this method is equivalent to callingmeta(String, String)
with"searchScore"
as the second argument.- Parameters:
fieldName
- the field name- Returns:
- the projection
- Since:
- 4.7
- MongoDB Atlas documentation
- Scoring
-
metaVectorSearchScore
Creates a projection to the given field name of the vectorSearchScore, for use withAggregates.vectorSearch(FieldSearchPath, Iterable, String, long, VectorSearchOptions)
. Calling this method is equivalent to callingmeta(String, String)
with"vectorSearchScore"
as the second argument. -
metaSearchHighlights
Creates a projection to the given field name of the searchHighlights, for use withAggregates.search(SearchOperator, SearchOptions)
/Aggregates.search(SearchCollector, SearchOptions)
. Calling this method is equivalent to callingmeta(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
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
-