Skip to content

Commit 53751bb

Browse files
authored
DOCSP-41135: Specify Fields (#8)
1 parent 1db6789 commit 53751bb

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

source/includes/read/project.kt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.example
2+
import com.mongodb.ConnectionString
3+
import com.mongodb.MongoClientSettings
4+
import com.mongodb.client.model.Filters.eq
5+
import com.mongodb.client.model.Projections
6+
import com.mongodb.kotlin.client.MongoClient
7+
import org.bson.codecs.pojo.annotations.BsonId
8+
import org.bson.types.ObjectId
9+
10+
// start-data-class
11+
data class Restaurant(
12+
@BsonId
13+
val id: ObjectId? = null,
14+
val name: String,
15+
val borough: String,
16+
val cuisine: String
17+
)
18+
// end-data-class
19+
20+
fun main() {
21+
val uri = "<connection string URI>"
22+
23+
val settings = MongoClientSettings.builder()
24+
.applyConnectionString(ConnectionString(uri))
25+
.retryWrites(true)
26+
.build()
27+
28+
val mongoClient = MongoClient.create(settings)
29+
val database = mongoClient.getDatabase("sample_restaurants")
30+
val collection = database.getCollection<Restaurant>("restaurants")
31+
32+
// start-project
33+
val projection = Projections.fields(
34+
Projections.include(
35+
Restaurant::name.name,
36+
Restaurant::cuisine.name,
37+
Restaurant::borough.name
38+
)
39+
)
40+
41+
val results = collection
42+
.find(eq(Restaurant::name.name, "Emerald Pub"))
43+
.projection(projection)
44+
45+
results.forEach { result ->
46+
println(result)
47+
}
48+
// end-project
49+
50+
// start-project-exclude
51+
val projection = Projections.fields(
52+
Projections.excludeId(),
53+
Projections.include(
54+
Restaurant::name.name,
55+
Restaurant::cuisine.name,
56+
Restaurant::borough.name
57+
)
58+
)
59+
60+
val results = collection
61+
.find(eq(Restaurant::name.name, "Emerald Pub"))
62+
.projection(projection)
63+
64+
results.forEach { result ->
65+
println(result)
66+
}
67+
// end-project-exclude
68+
}

source/read.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Read Data from MongoDB
2424

2525
/read/retrieve
2626
/read/count
27+
/read/project
2728

2829
Overview
2930
--------

source/read/project.txt

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
.. _kotlin-sync-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 ``restaurants`` collection in the ``sample_restaurants``
31+
database 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+
:atlas:`Get Started with Atlas </getting-started>` guide.
34+
35+
The documents in this collection are modeled by the following {+language+} data class:
36+
37+
.. literalinclude:: /includes/read/project.kt
38+
:start-after: start-data-class
39+
:end-before: end-data-class
40+
:language: kotlin
41+
:copyable:
42+
43+
Projection Types
44+
----------------
45+
46+
You can use a projection to specify which fields to include in a return
47+
document, or to specify which fields to exclude.
48+
49+
When specifying certain fields to include in a projection, all other fields are implicitly
50+
excluded (except the ``_id`` field, which is included by default). You cannot combine
51+
inclusion and exclusion statements in a single projection, unless you are excluding the
52+
``_id`` field.
53+
54+
To remove the ``_id`` field from the returned document, you must
55+
:ref:`explicitly exclude it <kotlin-sync-project-exclude-id>`.
56+
57+
Specify Fields to Include
58+
~~~~~~~~~~~~~~~~~~~~~~~~~
59+
60+
Use the following syntax to specify the fields to include in the result:
61+
62+
.. code-block:: kotlin
63+
64+
val projection = Projection.fields(
65+
Projections.include(<fieldName1>, <fieldName2>, ...)
66+
)
67+
68+
The following example uses the ``find()`` method to find all restaurants with
69+
the ``name`` field value of ``"Emerald Pub"``. It then uses a projection to
70+
return only the ``name``, ``cuisine``, and ``borough`` fields of the returned
71+
documents.
72+
73+
.. io-code-block::
74+
:copyable: true
75+
76+
.. input:: /includes/read/project.kt
77+
:start-after: start-project
78+
:end-before: end-project
79+
:language: kotlin
80+
:dedent:
81+
82+
.. output::
83+
:visible: false
84+
85+
Restaurant(id=5eb3d668b31de5d588f429e2, name=Emerald Pub, borough=Manhattan, cuisine=American)
86+
Restaurant(id=5eb3d668b31de5d588f432dd, name=Emerald Pub, borough=Queens, cuisine=American)
87+
88+
.. _kotlin-sync-project-exclude-id:
89+
90+
Exclude the ``_id`` Field
91+
~~~~~~~~~~~~~~~~~~~~~~~~~
92+
93+
When specifying fields to include, you can also exclude the ``_id`` field from
94+
the returned document.
95+
96+
The following example runs the same query as the preceding example, but
97+
excludes the ``_id`` field from the projection:
98+
99+
.. io-code-block::
100+
:copyable: true
101+
102+
.. input:: /includes/read/project.kt
103+
:start-after: start-project-exclude
104+
:end-before: end-project-exclude
105+
:language: kotlin
106+
:dedent:
107+
108+
.. output::
109+
:visible: false
110+
111+
Restaurant(id=null, name=Emerald Pub, borough=Manhattan, cuisine=American)
112+
Restaurant(id=null, name=Emerald Pub, borough=Queens, cuisine=American)
113+
114+
Additional Information
115+
----------------------
116+
117+
To learn more about projections, see the :manual:`Project Fields guide
118+
</tutorial/project-fields-from-query-results/>` in the MongoDB Server Manual.
119+
120+
API Documentation
121+
~~~~~~~~~~~~~~~~~
122+
123+
To learn more about any of the methods or types discussed in this
124+
guide, see the following API Documentation:
125+
126+
- `find() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/find.html>`__
127+
- `projection() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/projection.html>`__

0 commit comments

Comments
 (0)