1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
+++
date = "2015-03-19T14:27:51-04:00"
title = "Projections"
[menu.main]
parent = "Builders"
weight = 20
pre = "<i class='fa'></i>"
+++
## Projections
The [`Projections`]({{< apiref "com/mongodb/client/model/Projections" >}}) class provides static factory methods for all the MongoDB
projection opererators. Each method returns an instance of the [`Bson`]({{< relref "bson/documents.md#bson" >}}) type, which can in turn
be passed to any method that expects a projection.
For brevity, you may choose to import the methods of the `Projections` class statically:
```java
import static com.mongodb.client.model.Projections.*;
```
All the examples below assume this static import.
### Inclusion
By default, all fields of each document are projected. To specify the inclusion of one or more fields (which implicitly excludes all
other fields except `_id`), use the `include` method.
This example includes the `quantity` field and (implicitly) the `_id` field:
```java
include("quantity")
```
This example includes the `quantity` and `totalAmount` fields and (implicitly) the `_id` field:
```java
include("quantity", "totalAmount")
```
### Exclusion
To specify the exclusion of one or more fields (which implicitly includes all other fields), use the `exclude` method.
This example excludes the `quantity` field:
```java
exclude("quantity")
```
This example excludes the `quantity` and `totalAmount` fields:
```java
exclude("quantity", "totalAmount")
```
### Exclusion of _id
To specify the exclusion of the `_id` field, use the `excludeId` method:
```java
excludeId()
```
which is just shorthand for:
```java
exclude("_id")
```
### Array Element Match with a Supplied Filter
To specify a projection that includes only the first element of an array that matches a supplied query filter (the
[elemMatch]({{< docsref "reference/operator/projection/elemMatch" >}}) operator), use the `elemMatch` method that takes a
field name and a filter.
This example projects the first element of the `orders` array where the `quantity` field is greater that `3`:
```java
elemMatch("orders", Filters.gt("quantity", 3))
```
### Array Element Match with an Implicit Filter
To specify a projection that includes only the first element of an array that matches the filter supplied as part of the query (the
[positional $ operator]({{< docsref "reference/operator/projection/positional/#projection" >}})), use the `elemMatch` method that takes
just a field name.
This example projects the first element of the `orders` array that matches the query filter:
```java
elemMatch("orders")
```
### Slice
To project [a slice of an array]({{< docsref "reference/operator/projection/slice" >}}), use one of the `slice` methods.
This example projects the first `7` elements of the `tags` array:
```java
slice("tags", 7)
```
This example skips the first `2` elements of the `tags` array and projects the next `5`:
```java
slice("tags", 2, 5)
```
### Text Score
To specify a projection of [the score of a `$text` query]({{< docsref "reference/operator/query/text/#return-the-text-search-score" >}}),
use the `metaTextScore` method to specify the name of the projected field.
This example projects the text score as the value of the `score` field:
```java
metaTextScore("score")
```
### Combining Projections
To combine multiple projections, use the `fields` method.
This example includes the `quantity` and `totalAmount` fields and excludes the `_id` field:
```java
fields(include("quantity", "totalAmount"), excludeId())
```
|