@@ -11,3 +11,308 @@ Projections Builders
1111 :class: singlecol
1212
1313.. _projections-builders:
14+
15+ Overview
16+ --------
17+
18+ MongoDB supports **field projection**, specifying which fields to include and exclude when returning results from a
19+ query. Projection in MongoDB follows some basic rules:
20+
21+ - The ``_id`` field is **always** included unless explicitly excluded
22+ - Specifying a field for inclusion implicitly excludes all other fields **except** the ``_id`` field
23+ - Specifying a field for exclusion removes **only** that field in a query result
24+
25+ Find more information about projection mechanics :manual:`here </tutorial/project-fields-from-query-results/>`.
26+
27+ The :java-core-api:`Projections <com/mongodb/client/model/Projections.html>` class provides static factory methods for
28+ all the MongoDB projection operators. Each method returns an instance of the :ref:`Bson <bson>` type which you can pass
29+ to any method that expects a projection.
30+
31+ .. tip::
32+
33+ For brevity, you may choose to import the methods of the ``Projections`` class statically:
34+
35+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
36+ :start-after: begin static import
37+ :end-before: end static import
38+ :language: java
39+ :dedent:
40+
41+ The examples below assume this static import.
42+
43+ The collection is set up with the following documents:
44+
45+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
46+ :start-after: begin collection setup
47+ :end-before: end collection setup
48+ :language: java
49+ :dedent:
50+
51+ Inclusion
52+ ---------
53+
54+ Use the ``include()`` method to specify the inclusion of one or more fields.
55+
56+ The following example includes the ``year`` field and (implicitly) the ``_id`` field:
57+
58+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
59+ :start-after: begin include single field
60+ :end-before: end include single field
61+ :language: java
62+ :dedent:
63+
64+ The following code shows the output from this projection:
65+
66+ .. code-block:: json
67+ :copyable: false
68+
69+ {"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018}
70+ {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019}
71+
72+
73+ The following example includes the ``year`` and ``type`` fields and (implicitly) the ``_id`` field:
74+
75+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
76+ :start-after: begin include multiple fields
77+ :end-before: end include multiple fields
78+ :language: java
79+ :dedent:
80+
81+ The following code shows the output from this projection:
82+
83+ .. code-block:: json
84+ :copyable: false
85+
86+ {"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018, "type": "even number but not a leap year"}
87+ {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "type": "odd number, can't be a leap year"}
88+
89+ Exclusion
90+ ---------
91+
92+ Use the ``exclude()`` method to specify the exclusion of one or more fields.
93+
94+ The following example excludes the ``temperatures`` field:
95+
96+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
97+ :start-after: begin exclude one field
98+ :end-before: end exclude one field
99+ :language: java
100+ :dedent:
101+
102+ The following code shows the output from this projection:
103+
104+ .. code-block:: json
105+ :copyable: false
106+
107+ {"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018, "type": "even number but not a leap year"}
108+ {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "type": "odd number, can't be a leap year"}
109+
110+ The following example excludes the ``type`` and ``temperatures`` fields:
111+
112+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
113+ :start-after: begin exclude multiple fields
114+ :end-before: end exclude multiple fields
115+ :language: java
116+ :dedent:
117+
118+ The following code shows the output from this projection:
119+
120+ .. code-block:: json
121+ :copyable: false
122+
123+ {"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018}
124+ {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019}
125+
126+
127+ Combining Projections
128+ ---------------------
129+
130+ Use the ``fields()`` method to combine multiple projections.
131+
132+ The following example includes the ``year`` and ``type`` fields and excludes the
133+ ``_id`` field:
134+
135+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
136+ :start-after: begin show fields
137+ :end-before: end show fields
138+ :language: java
139+ :dedent:
140+
141+ The following code shows the output from this projection:
142+
143+ .. code-block:: json
144+ :copyable: false
145+
146+ {"year": 2018, "type": "even number but not a leap year"}
147+ {"year": 2019, "type": "odd number, can't be a leap year"}
148+
149+ Exclusion of ``_id``
150+ --------------------
151+
152+ Use the ``excludeId()`` convenience method to specify the exclusion of the ``_id`` field:
153+
154+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
155+ :start-after: begin exclude id
156+ :end-before: end exclude id
157+ :language: java
158+ :dedent:
159+
160+ The following code shows the output from this projection:
161+
162+ .. code-block:: json
163+ :copyable: false
164+
165+ {"year": 2018, "type": "even number but not a leap year"}
166+ {"year": 2019, "type": "odd number, can't be a leap year"}
167+
168+
169+ Project an Array Element Match
170+ ------------------------------
171+
172+ Use the ``elemMatch(String, Bson)`` method variant to specify an array projection that will include the first
173+ element of an array that matches a supplied query filter. This filtering occurs **after** all documents matching the
174+ query filter (if supplied) are retrieved.
175+
176+ .. note::
177+
178+ Only the first element that matches the specified query filter will be included, regardless of how many matches there
179+ may be.
180+
181+ The following example projects the first element of the ``temperatures`` array where the ``avg`` field is
182+ greater that ``10.1``:
183+
184+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
185+ :start-after: begin elemMatch no filter
186+ :end-before: end elemMatch no filter
187+ :language: java
188+ :dedent:
189+
190+ The following code shows the output from this projection:
191+
192+ .. code-block:: json
193+ :copyable: false
194+
195+ {"_id": {"$oid": "6042edc9f2b56342164e0790"}, "year": 2018}
196+ {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "temperatures": [{"month": "March", "avg": 10.43}]}
197+
198+
199+
200+ When you've specified matching criteria in the **query** portion of your operation, use the ``elemMatch(String)`` method
201+ variant to specify a :manual:`positional projection </reference/operator/projection/positional/#projection/>` to include
202+ the first element of an array. Only documents that match the query filter will be retrieved.
203+
204+ .. warning::
205+
206+ In MongoDB versions < 4.4, the specified array field must appear in the query document. Beginning in MongoDB 4.4,
207+ you can use a positional project on an array field that does not appear in the query document.
208+
209+
210+ The following example projects the first element of the ``temperatures`` array:
211+
212+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
213+ :start-after: begin elemMatch with filter
214+ :end-before: end elemMatch with filter
215+ :language: java
216+ :dedent:
217+
218+ The following code shows the output from this projection:
219+
220+ .. code-block:: json
221+ :copyable: false
222+
223+ {"_id": {"$oid": "6042edc9f2b56342164e0791"}, "year": 2019, "temperatures": [{"month": "March", "avg": 10.43}]}
224+
225+ Slice
226+ -----
227+
228+ Use the ``slice()`` method to project a :manual:`slice </reference/operator/projection/slice/>` of an array.
229+
230+ The following example projects the first **6** elements of the ``temperatures`` array:
231+
232+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
233+ :start-after: begin slice no skip
234+ :end-before: end slice no skip
235+ :language: java
236+ :dedent:
237+
238+ The following code shows the output from this projection:
239+
240+ .. code-block:: json
241+ :copyable: false
242+
243+ {
244+ "_id": {
245+ "$oid": "6042f1bc8ee6fa2a84d2be69"
246+ },
247+ "year": 2018,
248+ "type": "even number but not a leap year",
249+ "temperatures": [
250+ ... <January-June temperature subdocuments>
251+ ]
252+ }
253+ {
254+ "_id": {
255+ "$oid": "6042f1bc8ee6fa2a84d2be6a"
256+ },
257+ "year": 2019,
258+ "type": "odd number, can't be a leap year",
259+ "temperatures": [
260+ ... <January-June temperature subdocuments>
261+ ]
262+ }
263+
264+ The following example skips the first **6** elements of the ``temperatures`` array and projects the next **6**:
265+
266+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
267+ :start-after: begin slice with skip
268+ :end-before: end slice with skip
269+ :language: java
270+ :dedent:
271+
272+ The following code shows the output from this projection:
273+
274+ .. code-block:: json
275+ :copyable: false
276+
277+ {
278+ "_id": {
279+ "$oid": "6042f1bc8ee6fa2a84d2be69"
280+ },
281+ "year": 2018,
282+ "type": "even number but not a leap year",
283+ "temperatures": [
284+ ... <July-December temperature subdocuments>
285+ ]
286+ }
287+ {
288+ "_id": {
289+ "$oid": "6042f1bc8ee6fa2a84d2be6a"
290+ },
291+ "year": 2019,
292+ "type": "odd number, can't be a leap year",
293+ "temperatures": [
294+ ... <July-December temperature subdocuments>
295+ ]
296+ }
297+
298+ Text Score
299+ ----------
300+
301+ Use the ``metaTextScore()`` method to specify a projection of
302+ :manual:`score of a text query </reference/operator/query/text/#return-the-text-search-score/>`
303+
304+ The following example projects the text score as the value of the ``score`` field:
305+
306+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Projections.java
307+ :start-after: begin meta text score
308+ :end-before: end meta text score
309+ :language: java
310+ :dedent:
311+
312+ The following code shows the output from this projection:
313+
314+ .. code-block:: json
315+ :copyable: false
316+
317+ {"_id": {"$oid": "6042f1bc8ee6fa2a84d2be69"}, "year": 2018, "score": 1.25}
318+ {"_id": {"$oid": "6042f1bc8ee6fa2a84d2be6a"}, "year": 2019, "score": 0.625}
0 commit comments