@@ -21,27 +21,44 @@ document from read operations.
21
21
Sample Data
22
22
~~~~~~~~~~~
23
23
24
- To run the examples in this guide, load these documents into the
25
- ``tea.ratings`` collection with the following
26
- snippet:
24
+ The examples in this guide use the following ``Course`` struct as a model for documents
25
+ in the ``courses`` collection:
27
26
28
- .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/projection.go
27
+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/projection.go
28
+ :start-after: start-course-struct
29
+ :end-before: end-course-struct
30
+ :language: go
31
+ :dedent:
32
+
33
+ The ``omitempty`` :ref:`struct tag<golang-struct-tags>` directs the
34
+ driver to exclude fields when unmarshalling based on your projection
35
+ specification.
36
+
37
+ To run the examples in this guide, load the sample data into the
38
+ ``db.courses`` collection with the following snippet:
39
+
40
+ .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/projection.go
29
41
:language: go
30
42
:dedent:
31
43
:start-after: begin insertDocs
32
44
:end-before: end insertDocs
33
45
34
- .. include:: /includes/fundamentals/tea-sample-data-ending.rst
46
+ .. include:: /includes/fundamentals/automatic-db-coll-creation.rst
47
+
48
+ Each document contains a description of a university course that
49
+ includes the course title, course ID, and maximum enrollment, corresponding to
50
+ the ``title``, ``course_id``, and ``enrollment`` fields in each document.
35
51
36
52
Projection
37
53
----------
38
54
39
- A projection specifies which fields to return in matched documents. It
40
- contains field names followed by a ``1`` (to include) or ``0`` (to
41
- exclude). Projections can only include or exclude fields.
55
+ A projection specifies which fields to return in matched documents. The
56
+ projection document contains field names with a ``1`` to include the
57
+ corresponding field or ``0`` to exclude it. If you are using an aggregation framework,
58
+ you can also specify a projection to include newly computed fields.
42
59
43
- You can specify a projection by passing one to the ``SetProjection()``
44
- method in the options of the following read operation methods :
60
+ You can specify a projection by passing a projection document to the ``SetProjection()``
61
+ method. The following read operations take an options object as a parameter :
45
62
46
63
- ``Find()``
47
64
- ``FindOne()``
@@ -57,149 +74,134 @@ method in the options of the following read operation methods:
57
74
Exclude a Field
58
75
~~~~~~~~~~~~~~~
59
76
60
- To exclude a field, pass the field you want to exclude and a ``0`` to
61
- the ``SetProjection()`` method. For all fields you don't explicitly
62
- list in the projection, the driver includes them.
77
+ To exclude a field, pass the field you want to exclude with a ``0`` to
78
+ the ``SetProjection()`` method. The driver includes all fields that are
79
+ not explicitly excluded in the projection document, if you specify any
80
+ fields to exclude.
63
81
64
82
Example
65
83
```````
66
84
67
- The following example excludes the ``rating `` from the matched documents
68
- from the ``Find()`` method:
85
+ The following example excludes the ``course_id `` and ``enrollment``
86
+ fields from the matched documents returned by the ``Find()`` method:
69
87
70
88
.. io-code-block::
71
89
:copyable: true
72
90
73
91
.. input::
74
92
:language: go
75
93
76
- opts := options.Find().SetProjection(bson.D{{"rating", 0}})
77
-
78
- cursor, err := coll.Find(context.TODO(), bson.D{}, opts)
94
+ filter := bson.D{}
95
+ opts := options.Find().SetProjection(bson.D{{"course_id", 0}, {"enrollment", 0}})
96
+
97
+ cursor, err := coll.Find(context.TODO(), filter, opts)
79
98
if err != nil {
80
- panic(err)
99
+ panic(err)
81
100
}
82
-
83
- var results []bson.D
101
+
102
+ var results []Course
84
103
if err = cursor.All(context.TODO(), &results); err != nil {
85
- panic(err)
104
+ panic(err)
86
105
}
106
+
87
107
for _, result := range results {
88
- fmt.Println(result)
108
+ res, _ := bson.MarshalExtJSON(result, false, false)
109
+ fmt.Println(string(res))
89
110
}
90
111
91
112
.. output::
92
113
:language: none
93
114
:visible: false
94
115
95
- //results truncated
96
- [{_id ObjectID("...")} {type Masala}]
97
- [{_id ObjectID("...")} {type Assam}]
98
- [{_id ObjectID("...")} {type Oolong}]
99
- [{_id ObjectID("...")} {type Earl Grey}]
100
- [{_id ObjectID("...")} {type English Breakfast}]
116
+ {"title":"Primate Behavior"}
117
+ {"title":"Revolution and Reform"}
101
118
102
119
Include a Field
103
120
~~~~~~~~~~~~~~~
104
121
105
- To include a field, pass the field you want to include and a ``1`` to
106
- the ``SetProjection()`` method. For all fields you don't explicitly
107
- list in the projection, the driver excludes them.
108
-
109
- .. important::
110
-
111
- You can exclude the ``_id`` field even if you specified to include
112
- certain fields. By default, the driver includes the ``_id`` field.
113
- You must explicitly exclude the ``_id`` field if you do not want it
114
- returned.
122
+ To include a field, pass the field you want to include with a ``1`` to
123
+ the ``SetProjection()`` method. The driver excludes all fields that are
124
+ not explicitly included in the projection document, if you specify any
125
+ fields to include.
115
126
116
127
.. _golang-include-projection:
117
128
118
129
Example
119
130
```````
120
131
121
- The following example performs the following projection on the matched
122
- documents from the ``Find()`` method:
123
-
124
- - Include the ``type`` and ``rating`` field
125
- - Exclude the ``_id`` field
132
+ The following example includes only the ``title`` and ``enrollment`` fields
133
+ from the matched documents returned by the ``Find()`` method:
126
134
127
135
.. io-code-block::
128
136
:copyable: true
129
137
130
138
.. input::
131
139
:language: go
132
140
133
- opts := options.Find().SetProjection(bson.D{{"type", 1}, {"rating", 1}, {"_id", 0}})
134
-
135
- cursor, err := coll.Find(context.TODO(), bson.D{}, opts)
141
+ filter := bson.D{}
142
+ opts := options.Find().SetProjection(bson.D{{"title", 1}, {"enrollment", 1}})
143
+
144
+ cursor, err := coll.Find(context.TODO(), filter, opts)
136
145
if err != nil {
137
- panic(err)
146
+ panic(err)
138
147
}
139
-
140
- var results []bson.D
148
+
149
+ var results []Course
141
150
if err = cursor.All(context.TODO(), &results); err != nil {
142
- panic(err)
151
+ panic(err)
143
152
}
144
153
for _, result := range results {
145
- fmt.Println(result)
154
+ res, _ := bson.MarshalExtJSON(result, false, false)
155
+ fmt.Println(string(res))
146
156
}
147
157
148
158
.. output::
149
159
:language: none
150
160
:visible: false
151
161
152
- [{type Masala} {rating 10}]
153
- [{type Assam} {rating 5}]
154
- [{type Oolong} {rating 7}]
155
- [{type Earl Grey} {rating 8}]
156
- [{type English Breakfast} {rating 5}]
162
+ {"title":"Primate Behavior","enrollment":40}
163
+ {"title":"Revolution and Reform","enrollment":12}
157
164
158
165
Aggregation
159
166
~~~~~~~~~~~
160
167
161
- You can also include the :manual:`$project </reference/operator/aggregation/project/>`
168
+ You can also create a :manual:`$project </reference/operator/aggregation/project/>`
162
169
stage to specify a projection in an aggregation pipeline.
163
170
164
171
Example
165
172
```````
166
173
167
- The following example performs the following projection on the matched
168
- documents from the ``Aggregate()`` method:
169
-
170
- - Include the ``type`` and ``rating`` field
171
- - Exclude the ``_id`` field
174
+ The following example includes only the ``title`` and ``course_id`` fields
175
+ from the matched documents returned by the ``Aggregate()`` method:
172
176
173
177
.. io-code-block::
174
178
:copyable: true
175
179
176
180
.. input::
177
181
:language: go
178
182
179
- projectStage := bson.D{{"$project", bson.D{{"type ", 1}, {"rating ", 1}, {"_id", 0 }}}}
180
-
183
+ projectStage := bson.D{{"$project", bson.D{{"title ", 1}, {"course_id ", 1}}}}
184
+
181
185
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{projectStage})
182
186
if err != nil {
183
- panic(err)
187
+ panic(err)
184
188
}
185
-
186
- var results []bson.D
189
+
190
+ var results []Course
187
191
if err = cursor.All(context.TODO(), &results); err != nil {
188
- panic(err)
192
+ panic(err)
189
193
}
190
194
for _, result := range results {
191
- fmt.Println(result)
195
+ res, _ := bson.MarshalExtJSON(result, false, false)
196
+ fmt.Println(string(res))
192
197
}
193
198
194
199
.. output::
195
200
:language: none
196
201
:visible: false
197
202
198
- [{type Masala} {rating 10}]
199
- [{type Assam} {rating 5}]
200
- [{type Oolong} {rating 7}]
201
- [{type Earl Grey} {rating 8}]
202
- [{type English Breakfast} {rating 5}]
203
+ {"title":"Primate Behavior","course_id":"PSY2030"}
204
+ {"title":"Revolution and Reform","course_id":"HIST3080"}
203
205
204
206
Additional Information
205
207
----------------------
0 commit comments