File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,36 @@ collection.
53
53
54
54
To learn more about query filters, see the :ref:`kotlin-sync-specify-query` guide.
55
55
56
+ Find One Document Example
57
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
58
+
59
+ The following example chains the ``first()`` method to the ``find()`` method call to find
60
+ the first document in which the value of the ``cuisine`` field is ``"Spanish"``:
61
+
62
+ .. literalinclude:: /includes/read/retrieve.kt
63
+ :start-after: start-find-one
64
+ :end-before: end-find-one
65
+ :language: kotlin
66
+ :copyable:
67
+ :dedent:
68
+
69
+ The ``find()`` operation in the preceding example returns a MongoDB document
70
+ which you can print, as shown in the following example:
71
+
72
+ .. io-code-block::
73
+ :copyable: true
74
+
75
+ .. input:: /includes/read/retrieve.kt
76
+ :start-after: start-find-iterate
77
+ :end-before: end-find-iterate
78
+ :language: kotlin
79
+ :dedent:
80
+
81
+ .. output::
82
+ :visible: false
83
+
84
+ Restaurant(name=Tropicoso Club, cuisine=Spanish)
85
+
56
86
Find Documents Example
57
87
~~~~~~~~~~~~~~~~~~~~~~
58
88
Original file line number Diff line number Diff line change @@ -26,13 +26,22 @@ fun main() {
26
26
val results = collection.find(eq(Restaurant ::cuisine.name, " Spanish" ))
27
27
// end-find
28
28
29
+ // start-find-one
30
+ val results = collection.find(eq(Restaurant ::cuisine.name, " Spanish" )).firstOrNull()
31
+ // end-find-one
32
+
29
33
// start-find-iterate
30
34
val results = collection.find(eq(Restaurant ::cuisine.name, " Spanish" ))
31
35
results.forEach { result ->
32
36
println (result)
33
37
}
34
38
// end-find-iterate
35
39
40
+ // start-find-one-print
41
+ val results = collection.find(eq(Restaurant ::cuisine.name, " Spanish" ))
42
+ println (results)
43
+ // end-find-one-print
44
+
36
45
// start-find-all
37
46
val results = collection.find()
38
47
// end-find-all
You can’t perform that action at this time.
0 commit comments