Skip to content

Commit 84b6f3f

Browse files
authored
DOCSP-38759: projections builder (#28)
* DOCSP-38759: projections builder * fix tags
1 parent 2cd338a commit 84b6f3f

File tree

2 files changed

+157
-1
lines changed

2 files changed

+157
-1
lines changed

source/builders.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ Builders
77
.. toctree::
88

99
/builders/filters/
10+
/builders/projections/
1011

1112
The {+driver-short+} provides the following classes that make it easier to use
1213
the CRUD API:
1314

1415
- :ref:`scala-builders-filters`: Support for building query filters
16+
- :ref:`scala-builders-projections`: Support for building projections
1517

1618
.. TODO replace with links
1719

18-
- Projections: Support for building projections
1920
- Sorts: Support for building sort criteria
2021
- Aggregation: Support for building aggregation pipelines
2122
- Updates: Support for building updates

source/builders/projections.txt

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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

Comments
 (0)