|
| 1 | +.. _pymongo-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 ``sample_restaurants.restaurants`` collection |
| 31 | +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 | +:ref:`<pymongo-get-started>` guide. |
| 34 | + |
| 35 | +Projection Types |
| 36 | +---------------- |
| 37 | + |
| 38 | +You can use a projection to specify which fields to include in a return |
| 39 | +document, or to specify which fields to exclude. You cannot combine inclusion and |
| 40 | +exclusion statements in a single projection, unless you are excluding the |
| 41 | +``_id`` field. |
| 42 | + |
| 43 | +Specify Fields to Include |
| 44 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 45 | + |
| 46 | +Use the following syntax to specify the fields to include in the result: |
| 47 | + |
| 48 | +.. code-block:: json |
| 49 | + |
| 50 | + { "<Field Name>": 1 } |
| 51 | + |
| 52 | +The following example uses the ``find()`` method to find all restaurants with |
| 53 | +the ``name`` field value of ``"Emerald Pub"``. It then uses a projection to |
| 54 | +return only the ``name``, ``cuisine``, and ``borough`` fields in the returned |
| 55 | +documents. |
| 56 | + |
| 57 | +.. io-code-block:: |
| 58 | + |
| 59 | + .. input:: /includes/project/project.py |
| 60 | + :start-after: start-project-include |
| 61 | + :end-before: end-project-include |
| 62 | + :language: python |
| 63 | + :emphasize-lines: 1 |
| 64 | + |
| 65 | + .. output:: |
| 66 | + |
| 67 | + {'_id': ObjectId('...'), 'borough': 'Manhattan', 'cuisine': 'American', 'name': 'Emerald Pub'} |
| 68 | + {'_id': ObjectId('...'), 'borough': 'Queens', 'cuisine': 'American', 'name': 'Emerald Pub'} |
| 69 | + |
| 70 | +When you use a projection to specify fields to include in the return |
| 71 | +document, the ``_id`` field is also included by default. All other fields are |
| 72 | +implicitly excluded. To remove the ``_id`` field from the return |
| 73 | +document, you must :ref:`explicitly exclude it <pymongo-project-remove-id>`. |
| 74 | + |
| 75 | +.. _pymongo-project-remove-id: |
| 76 | + |
| 77 | +Exclude the ``_id`` Field |
| 78 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 79 | + |
| 80 | +When specifying fields to include, you can also exclude the ``_id`` field from |
| 81 | +the returned document. |
| 82 | + |
| 83 | +The following example performs the same query as the preceding example, but |
| 84 | +excludes the ``_id`` field from the projection: |
| 85 | + |
| 86 | +.. io-code-block:: |
| 87 | + |
| 88 | + .. input:: /includes/project/project.py |
| 89 | + :start-after: start-project-include-without-id |
| 90 | + :end-before: end-project-include-without-id |
| 91 | + :language: python |
| 92 | + :emphasize-lines: 1 |
| 93 | + |
| 94 | + .. output:: |
| 95 | + |
| 96 | + {'borough': 'Manhattan', 'cuisine': 'American', 'name': 'Emerald Pub'} |
| 97 | + {'borough': 'Queens', 'cuisine': 'American', 'name': 'Emerald Pub'} |
| 98 | + |
| 99 | +Specify Fields to Exclude |
| 100 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 101 | + |
| 102 | +Use the following syntax to specify the fields to exclude from the result: |
| 103 | + |
| 104 | +.. code-block:: json |
| 105 | + |
| 106 | + { "<Field Name>": 0 } |
| 107 | + |
| 108 | +The following example uses the ``find()`` method to find all restaurants with |
| 109 | +the ``name`` field value of ``"Emerald Pub"``. It then uses a projection to |
| 110 | +exclude the ``grades`` and ``address`` fields from the returned documents: |
| 111 | + |
| 112 | +.. io-code-block:: |
| 113 | + |
| 114 | + .. input:: /includes/project/project.py |
| 115 | + :start-after: start-project-exclude |
| 116 | + :end-before: end-project-exclude |
| 117 | + :language: python |
| 118 | + :emphasize-lines: 1 |
| 119 | + |
| 120 | + .. output:: |
| 121 | + |
| 122 | + {'_id': ObjectId('...'), 'borough': 'Manhattan', 'cuisine': 'American', 'name': 'Emerald Pub', 'restaurant_id': '40367329'} |
| 123 | + {'_id': ObjectId('...'), 'borough': 'Queens', 'cuisine': 'American', |
| 124 | + 'name': 'Emerald Pub', 'restaurant_id': '40668598'} |
| 125 | + |
| 126 | +When you use a projection to specify which fields to exclude, |
| 127 | +any unspecified fields are implicitly included in the return document. |
| 128 | + |
| 129 | +Troubleshooting |
| 130 | +--------------- |
| 131 | + |
| 132 | +The following sections describe errors you might see when using projections. |
| 133 | + |
| 134 | +.. include:: /includes/troubleshooting/projections.rst |
| 135 | + |
| 136 | +Additional Information |
| 137 | +---------------------- |
| 138 | + |
| 139 | +To learn more about projections, see the :manual:`Project Fields guide |
| 140 | +</tutorial/project-fields-from-query-results/>` in the MongoDB Server Manual. |
| 141 | + |
| 142 | +API Documentation |
| 143 | +~~~~~~~~~~~~~~~~~ |
| 144 | + |
| 145 | +To learn more about any of the methods or types discussed in this |
| 146 | +guide, see the following API Documentation: |
| 147 | + |
| 148 | +- `find() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.find>`__ |
0 commit comments