|
| 1 | +.. _scala-builders-projections: |
| 2 | + |
| 3 | +=========== |
| 4 | +Projections |
| 5 | +=========== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, create fields, customize results |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +The ``Projections`` class provides static factory methods for the |
| 21 | +MongoDB projection operators. Each method returns an instance of the |
| 22 | +``Bson`` type, which can in turn be passed to any method that expects a |
| 23 | +projection. |
| 24 | + |
| 25 | +You can import the methods of the ``Filters`` |
| 26 | +class statically, as shown in the following code: |
| 27 | + |
| 28 | +.. code-block:: scala |
| 29 | + |
| 30 | + import org.mongodb.scala.model.Projections._ |
| 31 | + |
| 32 | +The examples in this guide assume this static import. |
| 33 | + |
| 34 | +Inclusion |
| 35 | +--------- |
| 36 | + |
| 37 | +By default, all fields of each document are included in the results. To specify the |
| 38 | +inclusion of one or more fields, which implicitly excludes all other |
| 39 | +fields except ``_id``, use the ``include()`` method. |
| 40 | + |
| 41 | +The following example includes the ``quantity`` field and, implicitly, the |
| 42 | +``_id`` field: |
| 43 | + |
| 44 | +.. code-block:: scala |
| 45 | + |
| 46 | + include("quantity") |
| 47 | + |
| 48 | +The following example includes the ``quantity`` and ``totalAmount`` |
| 49 | +fields and, implicitly, the ``_id`` field: |
| 50 | + |
| 51 | +.. code-block:: scala |
| 52 | + |
| 53 | + include("quantity", "totalAmount") |
| 54 | + |
| 55 | +Exclusion |
| 56 | +--------- |
| 57 | + |
| 58 | +To specify the exclusion of one or more fields, which implicitly |
| 59 | +includes all other fields, use the ``exclude()`` method. |
| 60 | + |
| 61 | +The following example excludes the ``quantity`` field: |
| 62 | + |
| 63 | +exclude("quantity") |
| 64 | +This example excludes the quantity and totalAmount fields: |
| 65 | + |
| 66 | +.. code-block:: scala |
| 67 | + |
| 68 | + exclude("quantity", "totalAmount") |
| 69 | + |
| 70 | +Exclusion of _id Field |
| 71 | +---------------------- |
| 72 | + |
| 73 | +To specify the exclusion of the ``_id`` field, use the ``excludeId()`` |
| 74 | +method: |
| 75 | + |
| 76 | +.. code-block:: scala |
| 77 | + |
| 78 | + excludeId() |
| 79 | + |
| 80 | +This is equivalent to the following code: |
| 81 | + |
| 82 | +.. code-block:: scala |
| 83 | + |
| 84 | + exclude("_id") |
| 85 | + |
| 86 | +Array Element Match with a Specified Filter |
| 87 | +------------------------------------------- |
| 88 | + |
| 89 | +To specify a projection that includes only the first element of an array |
| 90 | +that matches a supplied query filter, use the |
| 91 | +``elemMatch()`` method that takes a field name and a filter. |
| 92 | + |
| 93 | +The following example projects the first element of the ``orders`` array field, |
| 94 | +where the ``quantity`` field is greater than ``3``: |
| 95 | + |
| 96 | +.. code-block:: scala |
| 97 | + |
| 98 | + elemMatch("orders", Filters.gt("quantity", 3)) |
| 99 | + |
| 100 | +Array Element Match with an Implicit Filter |
| 101 | +------------------------------------------- |
| 102 | + |
| 103 | +To specify a projection that includes only the first element of an array |
| 104 | +that matches the filter supplied as part of the query, use the |
| 105 | +``elemMatch()`` method that takes just a field name. |
| 106 | + |
| 107 | +The following example projects the first element of the ``orders`` array |
| 108 | +that matches the query filter: |
| 109 | + |
| 110 | +.. code-block:: scala |
| 111 | + |
| 112 | + elemMatch("orders") |
| 113 | + |
| 114 | +Slice |
| 115 | +----- |
| 116 | + |
| 117 | +To project a slice of an array, use one of the ``slice()`` methods. |
| 118 | + |
| 119 | +The following example projects the first ``7`` elements of the ``tags`` array: |
| 120 | + |
| 121 | +.. code-block:: scala |
| 122 | + |
| 123 | + slice("tags", 7) |
| 124 | + |
| 125 | +The following example skips the first ``2`` elements of the ``tags`` |
| 126 | +array and projects the next ``5``: |
| 127 | + |
| 128 | +.. code-block:: scala |
| 129 | + |
| 130 | + slice("tags", 2, 5) |
| 131 | + |
| 132 | +Text Score |
| 133 | +---------- |
| 134 | + |
| 135 | +To specify a projection of the score of a ``$text`` query, use the |
| 136 | +``metaTextScore()`` method to specify the name of the projected field. |
| 137 | + |
| 138 | +The following example projects the text score as the value of the |
| 139 | +``score`` field: |
| 140 | + |
| 141 | +.. code-block:: scala |
| 142 | + |
| 143 | + metaTextScore("score") |
| 144 | + |
| 145 | +Combining Projections |
| 146 | +--------------------- |
| 147 | + |
| 148 | +To combine multiple projections, use the fields method. |
| 149 | + |
| 150 | +The following example includes the ``quantity`` and ``totalAmount`` |
| 151 | +fields and excludes the ``_id`` field: |
| 152 | + |
| 153 | +.. code-block:: scala |
| 154 | + |
| 155 | + fields(include("quantity", "totalAmount"), excludeId()) |
0 commit comments