@@ -16,7 +16,7 @@ Overview
1616--------
1717
1818In this guide, you can learn how to control which fields appear in
19- documents returned from read operations with the MongoDB Java driver.
19+ documents returned from read operations with the MongoDB Kotlin driver.
2020
2121Many read requests require only a subset of fields in a document.
2222For example, when logging a user in you may only need their username, and
@@ -63,69 +63,67 @@ varieties of fruit:
6363 { "_id": 3, "name": "oranges", "qty": 6, "rating": 2 },
6464 { "_id": 4, "name": "avocados", "qty": 3, "rating": 5 },
6565
66+
67+ This data is modeled using the following Kotlin data class:
68+
69+ .. literalinclude:: /examples/generated/ProjectTest.snippet.fruit-data-class.kt
70+ :language: kotlin
71+
6672In the following query, pass the projection to return the ``name``
67- field of each document:
73+ field of each document. The results are modeled using the ``FruitName`` Kotlin data class:
6874
69- .. code-block:: java
70- :emphasize-lines: 3
75+ .. io-code-block::
7176
72- // return all documents with *only* the name field
73- Bson filter = Filters.empty();
74- Bson projection = Projections.fields(Projections.include("name"));
75- collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc));
77+ .. input:: /examples/generated/ProjectTest.snippet.project-name.kt
78+ :language: kotlin
79+ :emphasize-lines: 8-10
7680
77- The projection document specifies that the read operation result should
81+ .. output::
82+ :language: console
83+
84+ FruitName(id=1, name=apples),
85+ FruitName(id=2, name=bananas),
86+ FruitName(id=3, name=oranges),
87+ FruitName(id=4, name=avocados)
88+
89+ The projection document specifies that the read operation result should
7890*include* the ``name`` field of each returned document. As a result, this
7991projection implicitly excludes the ``qty`` and ``rating`` fields. Chaining
8092this projection to ``find()`` with an empty query filter yields the
81- following results:
82-
83- .. code-block:: json
84- :copyable: false
85-
86- { "_id": 1, "name": "apples" }
87- { "_id": 2, "name": "bananas" }
88- { "_id": 3, "name": "oranges" }
89- { "_id": 4, "name": "avocados" }
93+ above results.
9094
9195Despite the fact that this projection only explicitly included the
92- ``name`` field, the query returned the ``_id`` field as well .
96+ ``name`` field, the query also returned the ``_id`` field, represented by ``id`` in the data class .
9397
9498The ``_id`` field is a special case: it is always included in every query
9599result unless explicitly excluded. That's because the ``_id`` field is a
96100unique identifier for each document, a property that can be useful when
97101constructing queries.
98102
99- A ``movies`` collection is a good example of why this property is useful:
100- because remakes and even separate works sometimes reuse movie titles,
101- you need a unique ``_id`` value to refer to any specific movie.
102-
103103The ``_id`` is the only exception to the mutually exclusive include-exclude
104104behavior in projections: you *can* explicitly exclude the ``_id`` field
105105even when explicitly including other fields if you do not want ``_id``
106106to be present in returned documents.
107107
108- .. code-block:: java
109- :emphasize-lines: 3
108+ .. io-code-block::
109+
110+ .. input:: /examples/generated/ProjectTest.snippet.exclude-id.kt
111+ :language: kotlin
112+ :emphasize-lines: 9-12
110113
111- // return all documents with only the name field
112- Bson filter = Filters.empty();
113- Bson projection = Projections.fields(Projections.include("name"), Projections.excludeId());
114- collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc));
114+ .. output::
115+ :language: console
116+
117+ FruitName(name=apples),
118+ FruitName(name=bananas),
119+ FruitName(name=oranges),
120+ FruitName(name=avocados)
115121
116122The projection document specifies that the read operation result should
117123*include* the ``name`` field of each returned document, and specifies to
118124*exclude* the ``_id`` field. As a result, this projection implicitly
119125excludes the ``qty`` and ``rating`` fields. Chaining this projection to
120- ``find()`` with an empty query filter yields the following results:
121-
122- .. code-block:: javascript
123- :copyable: false
124-
125- { "name": "apples" }
126- { "name": "bananas" }
127- { "name": "oranges" }
128- { "name": "avocados" }
126+ ``find()`` with an empty query filter yields the above results.
129127
130128You can also specify multiple fields to include in your projection.
131129
@@ -134,23 +132,22 @@ You can also specify multiple fields to include in your projection.
134132 The order in which you specify the fields in the projection does not
135133 alter the order in which they are returned.
136134
137- .. code-block:: java
138- :emphasize-lines: 2
135+ This example that identifies two fields to include in the projection yields
136+ the following results using the ``FruitRating`` Kotlin data class:
139137
140- Bson filter = Filters.empty();
141- Bson projection = Projections.fields(Projections.include("name", "rating"), Projections.excludeId());
142- collection.find(filter).projection(projection).forEach(doc -> System.out.println(doc));
138+ .. io-code-block::
143139
144- This example that identifies two fields to include in the projection yields
145- the following results:
140+ .. input:: /examples/generated/ProjectTest.snippet.multiple-fields.kt
141+ :language: kotlin
142+ :emphasize-lines: 8-9
146143
147- .. code-block :: json
148- :copyable: false
144+ .. output ::
145+ :language: console
149146
150- { " name": " apples", " rating": 3 }
151- { " name": " bananas", " rating": 1 }
152- { " name": " oranges", " rating": 2 }
153- { " name": " avocados", " rating": 5 }
147+ FruitRating( name= apples, rating=3),
148+ FruitRating( name= bananas, rating=1),
149+ FruitRating( name= oranges, rating=2),
150+ FruitRating( name= avocados, rating=5)
154151
155152For additional projection examples, see the
156153:manual:`MongoDB Manual page on Project Fields to Return from Query </tutorial/project-fields-from-query-results/>`.
0 commit comments