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