|
| 1 | +.. _kotlin-sync-project: |
| 2 | + |
| 3 | +======================== |
| 4 | +Specify Fields To Return |
| 5 | +======================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: read, filter, project, select |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to specify which fields to return from a read |
| 24 | +operation by using a **projection**. A projection is a document that specifies |
| 25 | +which fields MongoDB returns from a query. |
| 26 | + |
| 27 | +Sample Data |
| 28 | +~~~~~~~~~~~ |
| 29 | + |
| 30 | +The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants`` |
| 31 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 32 | +free MongoDB Atlas cluster and load the sample datasets, see the |
| 33 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 34 | + |
| 35 | +The documents in this collection are modeled by the following {+language+} data class: |
| 36 | + |
| 37 | +.. literalinclude:: /includes/read/project.kt |
| 38 | + :start-after: start-data-class |
| 39 | + :end-before: end-data-class |
| 40 | + :language: kotlin |
| 41 | + :copyable: |
| 42 | + |
| 43 | +Projection Types |
| 44 | +---------------- |
| 45 | + |
| 46 | +You can use a projection to specify which fields to include in a return |
| 47 | +document, or to specify which fields to exclude. |
| 48 | + |
| 49 | +When specifying certain fields to include in a projection, all other fields are implicitly |
| 50 | +excluded (except the ``_id`` field, which is included by default). You cannot combine |
| 51 | +inclusion and exclusion statements in a single projection, unless you are excluding the |
| 52 | +``_id`` field. |
| 53 | + |
| 54 | +To remove the ``_id`` field from the returned document, you must |
| 55 | +:ref:`explicitly exclude it <kotlin-sync-project-exclude-id>`. |
| 56 | + |
| 57 | +Specify Fields to Include |
| 58 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 59 | + |
| 60 | +Use the following syntax to specify the fields to include in the result: |
| 61 | + |
| 62 | +.. code-block:: kotlin |
| 63 | + |
| 64 | + val projection = Projection.fields( |
| 65 | + Projections.include(<fieldName1>, <fieldName2>, ...) |
| 66 | + ) |
| 67 | + |
| 68 | +The following example uses the ``find()`` method to find all restaurants with |
| 69 | +the ``name`` field value of ``"Emerald Pub"``. It then uses a projection to |
| 70 | +return only the ``name``, ``cuisine``, and ``borough`` fields of the returned |
| 71 | +documents. |
| 72 | + |
| 73 | +.. io-code-block:: |
| 74 | + :copyable: true |
| 75 | + |
| 76 | + .. input:: /includes/read/project.kt |
| 77 | + :start-after: start-project |
| 78 | + :end-before: end-project |
| 79 | + :language: kotlin |
| 80 | + :dedent: |
| 81 | + |
| 82 | + .. output:: |
| 83 | + :visible: false |
| 84 | + |
| 85 | + Restaurant(id=5eb3d668b31de5d588f429e2, name=Emerald Pub, borough=Manhattan, cuisine=American) |
| 86 | + Restaurant(id=5eb3d668b31de5d588f432dd, name=Emerald Pub, borough=Queens, cuisine=American) |
| 87 | + |
| 88 | +.. _kotlin-sync-project-exclude-id: |
| 89 | + |
| 90 | +Exclude the ``_id`` Field |
| 91 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 92 | + |
| 93 | +When specifying fields to include, you can also exclude the ``_id`` field from |
| 94 | +the returned document. |
| 95 | + |
| 96 | +The following example runs the same query as the preceding example, but |
| 97 | +excludes the ``_id`` field from the projection: |
| 98 | + |
| 99 | +.. io-code-block:: |
| 100 | + :copyable: true |
| 101 | + |
| 102 | + .. input:: /includes/read/project.kt |
| 103 | + :start-after: start-project-exclude |
| 104 | + :end-before: end-project-exclude |
| 105 | + :language: kotlin |
| 106 | + :dedent: |
| 107 | + |
| 108 | + .. output:: |
| 109 | + :visible: false |
| 110 | + |
| 111 | + Restaurant(id=null, name=Emerald Pub, borough=Manhattan, cuisine=American) |
| 112 | + Restaurant(id=null, name=Emerald Pub, borough=Queens, cuisine=American) |
| 113 | + |
| 114 | +Additional Information |
| 115 | +---------------------- |
| 116 | + |
| 117 | +To learn more about projections, see the :manual:`Project Fields guide |
| 118 | +</tutorial/project-fields-from-query-results/>` in the MongoDB Server Manual. |
| 119 | + |
| 120 | +API Documentation |
| 121 | +~~~~~~~~~~~~~~~~~ |
| 122 | + |
| 123 | +To learn more about any of the methods or types discussed in this |
| 124 | +guide, see the following API Documentation: |
| 125 | + |
| 126 | +- `find() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/find.html>`__ |
| 127 | +- `projection() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/projection.html>`__ |
0 commit comments