|
| 1 | +============================== |
| 2 | +Specify Which Fields to Return |
| 3 | +============================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to specify which fields to return in a |
| 17 | +document from read operations. |
| 18 | + |
| 19 | +Sample Data |
| 20 | +~~~~~~~~~~~ |
| 21 | + |
| 22 | +To run the examples in this guide, load these documents into the |
| 23 | +``ratings`` collection of the ``tea`` database with the following |
| 24 | +snippet: |
| 25 | + |
| 26 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/projection.go |
| 27 | + :language: go |
| 28 | + :dedent: |
| 29 | + :start-after: begin insertDocs |
| 30 | + :end-before: end insertDocs |
| 31 | + |
| 32 | +.. include:: /includes/fundamentals/tea-sample-data-ending.rst |
| 33 | + |
| 34 | +Projection |
| 35 | +---------- |
| 36 | + |
| 37 | +A projection specifies which fields to return in matched documents. It |
| 38 | +contains field names followed by a ``1`` (to include) or ``0`` (to |
| 39 | +exclude). Projections can only include or exclude fields. |
| 40 | + |
| 41 | +You can specify a projection by passing one to the ``SetProjection()`` |
| 42 | +function in the options of the following read operation functions: |
| 43 | + |
| 44 | +- ``Find()`` |
| 45 | +- ``FindOne()`` |
| 46 | +- ``FindOneAndDelete()`` |
| 47 | +- ``FindOneAndReplace()`` |
| 48 | +- ``FindOneAndUpdate()`` |
| 49 | + |
| 50 | +.. tip:: |
| 51 | + |
| 52 | + If you don't specify a projection, the read operation returns all |
| 53 | + the fields in matched documents. |
| 54 | + |
| 55 | +Exclude a Field |
| 56 | +~~~~~~~~~~~~~~~ |
| 57 | + |
| 58 | +To exclude a field, pass the field you want to exclude and a ``0`` to |
| 59 | +the ``SetProjection()`` function. For all fields you don't explicitly |
| 60 | +list in the projection, the driver includes them. |
| 61 | + |
| 62 | +Example |
| 63 | +``````` |
| 64 | + |
| 65 | +The following example excludes the ``rating`` from the matched documents |
| 66 | +from the ``Find()`` function: |
| 67 | + |
| 68 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/projection.go |
| 69 | + :language: go |
| 70 | + :dedent: |
| 71 | + :start-after: begin exclude projection |
| 72 | + :end-before: end exclude projection |
| 73 | + |
| 74 | +After running this example, the output resembles the following: |
| 75 | + |
| 76 | +.. code-block:: none |
| 77 | + :copyable: false |
| 78 | + |
| 79 | + //results truncated |
| 80 | + [{_id ObjectID("...")} {type Masala}] |
| 81 | + [{_id ObjectID("...")} {type Assam}] |
| 82 | + [{_id ObjectID("...")} {type Oolong}] |
| 83 | + [{_id ObjectID("...")} {type Earl Grey}] |
| 84 | + [{_id ObjectID("...")} {type English Breakfast}] |
| 85 | + |
| 86 | +Include a Field |
| 87 | +~~~~~~~~~~~~~~~ |
| 88 | + |
| 89 | +To include a field, pass the field you want to include and a ``1`` to |
| 90 | +the ``SetProjection()`` function. For all fields you don't explicitly |
| 91 | +list in the projection, the driver excludes them. |
| 92 | + |
| 93 | +.. important:: |
| 94 | + |
| 95 | + You can exclude the ``_id`` field even if you specified to include |
| 96 | + certain fields. By default, the driver includes the ``_id`` field. |
| 97 | + You must explicitly exclude the ``_id`` field if you do not want it |
| 98 | + returned. |
| 99 | + |
| 100 | +.. _go-include-projection: |
| 101 | + |
| 102 | +Example |
| 103 | +``````` |
| 104 | + |
| 105 | +The following example performs the following projection on the matched |
| 106 | +documents from the ``Find()`` function: |
| 107 | + |
| 108 | +- Include the ``type`` and ``rating`` field |
| 109 | +- Exclude the ``_id`` field |
| 110 | + |
| 111 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/projection.go |
| 112 | + :language: go |
| 113 | + :dedent: |
| 114 | + :start-after: begin include projection |
| 115 | + :end-before: end include projection |
| 116 | + |
| 117 | +After running this example, the output resembles the following: |
| 118 | + |
| 119 | +.. code-block:: none |
| 120 | + :copyable: false |
| 121 | + |
| 122 | + [{type Masala} {rating 10}] |
| 123 | + [{type Assam} {rating 5}] |
| 124 | + [{type Oolong} {rating 7}] |
| 125 | + [{type Earl Grey} {rating 8}] |
| 126 | + [{type English Breakfast} {rating 5}] |
| 127 | + |
| 128 | +Aggregation |
| 129 | +~~~~~~~~~~~ |
| 130 | + |
| 131 | +You can also include the :manual:`$project </reference/operator/aggregation/project/>` |
| 132 | +stage to specify a projection in an aggregation pipeline. |
| 133 | + |
| 134 | +Example |
| 135 | +``````` |
| 136 | + |
| 137 | +The following example performs the following projection on the matched |
| 138 | +documents from the ``Aggregate()`` function: |
| 139 | + |
| 140 | +- Include the ``type`` and ``rating`` field |
| 141 | +- Exclude the ``_id`` field |
| 142 | + |
| 143 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/projection.go |
| 144 | + :language: go |
| 145 | + :dedent: |
| 146 | + :start-after: begin aggregate projection |
| 147 | + :end-before: end aggregate projection |
| 148 | + |
| 149 | +After running this example, the output resembles the following: |
| 150 | + |
| 151 | +.. code-block:: none |
| 152 | + :copyable: false |
| 153 | + |
| 154 | + [{type Masala} {rating 10}] |
| 155 | + [{type Assam} {rating 5}] |
| 156 | + [{type Oolong} {rating 7}] |
| 157 | + [{type Earl Grey} {rating 8}] |
| 158 | + [{type English Breakfast} {rating 5}] |
| 159 | + |
| 160 | +Additional Information |
| 161 | +---------------------- |
| 162 | + |
| 163 | +For more information on performing read operations, see our guide on |
| 164 | +:doc:`retrieving data </fundamentals/crud/read-operations/retrieve>`. |
| 165 | + |
| 166 | +.. For more information on aggregation, see the |
| 167 | +.. :doc:`Aggregation </fundamentals/aggregation>` guide. |
| 168 | + |
| 169 | +API Documentation |
| 170 | +~~~~~~~~~~~~~~~~~ |
| 171 | + |
| 172 | +For more information on any of the functions or types discussed in this |
| 173 | +guide, see the following API Documentation: |
| 174 | + |
| 175 | +- `Find() <{+api+}/mongo#Collection.Find>`__ |
| 176 | +- `FindOptions.SetProjection() <{+api+}/mongo/options#FindOptions.SetProjection>`__ |
| 177 | +- `FindOne() <{+api+}/mongo#Collection.FindOne>`__ |
| 178 | +- `FindOneAndDelete() <{+api+}/mongo#Collection.FindOneAndDelete>`__ |
| 179 | +- `FindOneAndReplace() <{+api+}/mongo#Collection.FindOneAndReplace>`__ |
| 180 | +- `FindOneAndUpdate() <{+api+}/mongo#Collection.FindOneAndUpdate>`__ |
| 181 | +- `Aggregate() <{+api+}/mongo#Collection.Aggregate>`__ |
0 commit comments