@@ -16,7 +16,7 @@ Overview
1616--------
1717
1818In this guide, you can learn how to limit the number of results returned
19- from read operations with the MongoDB Java driver.
19+ from read operations with the MongoDB Kotlin driver.
2020
2121Use ``limit()`` to cap the number of documents that a read operation returns.
2222This instance method designates the maximum number of
@@ -34,102 +34,73 @@ and how to combine ``limit()`` with ``skip()`` to further narrow the results ret
3434Sample Documents
3535~~~~~~~~~~~~~~~~
3636
37- The following operation inserts documents representing books into a collection:
38-
39- .. code-block:: java
40-
41- collection.insertMany(Arrays.asList(
42- new Document().append("_id", 1)
43- .append("title", "The Brothers Karamazov").append("length", 824)
44- .append("author", "Dostoyevsky"),
45- new Document().append("_id", 2)
46- .append("title", "Les Misérables").append("length", 1462).append("author", "Hugo"),
47- new Document().append("_id", 3)
48- .append("title", "Atlas Shrugged").append("length", 1088).append("author", "Rand"),
49- new Document().append("_id", 4)
50- .append("title", "Infinite Jest").append("length", 1104).append("author", "Wallace"),
51- new Document().append("_id", 5)
52- .append("title", "Cryptonomicon").append("length", 918).append("author", "Stephenson"),
53- new Document().append("_id", 6)
54- .append("title", "A Dance with Dragons").append("length", 1104)
55- .append("author", "Martin")
56- ));
37+ The following sections feature examples that update this sample document:
38+
39+ .. code-block:: json
40+
41+ { "_id": 1, "title": "The Brothers Karamazov", "author": "Dostoyevsky", "length": 824 }
42+ { "_id": 2, "title": "Les Misérables", "author": "Hugo", "length": 1462 }
43+ { "_id": 3, "title": "Atlas Shrugged", "author": "Rand", "length": 1088 }
44+ { "_id": 4, "title": "Infinite Jest", "author": "Wallace", "length": 1104 }
45+ { "_id": 5, "title": "Cryptonomicon", "author": "Stephenson", "length": 918 }
46+ { "_id": 6, "title": "A Dance with Dragons", "author": "Martin", "length": 1104 }
47+
48+ This data is modeled with the following Kotlin data class:
49+
50+ .. literalinclude:: /examples/generated/LimitTest.snippet.data-model.kt
51+ :language: kotlin
5752
5853Specify a Limit
5954---------------
6055
6156The next example queries the collection to return the top three
6257longest books. It first matches all the documents with the query, then sorts on the
6358``length`` field to return books with longer lengths before
64- books with shorter lengths. Lastly, it limits the return value to ``3`` documents:
65-
66- .. code-block:: java
67- :emphasize-lines: 6
68-
69- import com.mongodb.client.model.Sorts;
70-
71- // define a cursor that will return the first 3 sorted items
72- MongoCursor<Document> cursor = collection.find()
73- .sort(descending("length"))
74- .limit(3)
75- .iterator();
76- // print out items
77- try {
78- while (cursor.hasNext()) {
79- System.out.println(cursor.next());
80- }
81- }
82- // close the cursor
83- finally {
84- cursor.close();
85- }
86-
87- The preceding code example prints out the following three documents, sorted by
88- length:
89-
90- .. code-block:: java
91- :copyable: false
92-
93- Document{{_id=2, title=Les Misérables, author=Hugo, length=1462}}
94- Document{{_id=6, title=A Dance with Dragons, author=Martin, length=1104}}
95- Document{{_id=4, title=Infinite Jest, author=Wallace, length=1104}}
59+ books with shorter lengths. Lastly, it limits the return value to ``3`` documents,
60+ and returns the following three documents, sorted by length:
61+
62+ .. io-code-block::
63+
64+ .. input:: /examples/generated/LimitTest.snippet.specify-limit.kt
65+ :language: kotlin
66+ :emphasize-lines: 3
67+
68+ .. output::
69+ :language: console
70+
71+ Book(id=2, title=Les Misérables, author=Hugo, length=1462)
72+ Book(id=6, title=A Dance with Dragons, author=Martin, length=1104)
73+ Book(id=4, title=Infinite Jest, author=Wallace, length=1104)
9674
9775.. tip::
9876
9977 The order in which you call ``limit()`` and ``sort()`` does not matter
100- because the driver reorders the calls to apply the sort first and the
78+ because the find command always applies the sort first and the
10179 limit after it. The following two calls are equivalent:
10280
103- .. code-block:: java
104-
105- collection.find().sort(descending("length")).limit(3);
106- collection.find().limit(3).sort(descending("length"));
81+ .. literalinclude:: /examples/generated/LimitTest.snippet.equivalent.kt
82+ :language: kotlin
10783
10884Combining Skip and Limit
10985------------------------
11086
11187To see the next three longest books, append the ``skip()`` method to your
11288``find()`` call. The integer argument passed to ``skip()`` will determine
113- how many documents the find operation returns:
114-
115- .. code-block:: java
116- :emphasize-lines: 3, 4
89+ how many documents the find operation returns. This operation returns the
90+ documents that describe the fourth through sixth longest books:
11791
118- MongoCursor<Document> cursor = collection.find()
119- .sort(ascending("length"))
120- .limit(3)
121- .skip(3)
122- .iterator();
92+ .. io-code-block::
12393
124- This operation returns the documents that describe the fourth through sixth
125- longest books:
94+ .. input:: /examples/generated/LimitTest.snippet.skip-limit.kt
95+ :language: kotlin
96+ :emphasize-lines: 3, 4
12697
127- .. code-block :: java
128- :copyable: false
98+ .. output ::
99+ :language: console
129100
130- Document{{_id =3, title=Atlas Shrugged, author=Rand, length=1088}}
131- Document{{_id =5, title=Cryptonomicon, author=Stephenson, length=918}}
132- Document{{_id =1, title=The Brothers Karamazov, author=Dostoyevsky, length=824}}
101+ Book(id =3, title=Atlas Shrugged, author=Rand, length=1088)
102+ Book(id =5, title=Cryptonomicon, author=Stephenson, length=918)
103+ Book(id =1, title=The Brothers Karamazov, author=Dostoyevsky, length=824)
133104
134105You can combine ``skip()`` and ``limit()`` in this way to implement paging for your
135106collection, returning only small subsets of the collection at one time.
@@ -143,7 +114,7 @@ collection, returning only small subsets of the collection at one time.
143114
144115 For example, consider the following data:
145116
146- .. code-block:: java
117+ .. code-block:: json
147118 :copyable: false
148119
149120 { type: "computer", data: "1", serial_no: 235235 }
@@ -159,7 +130,7 @@ collection, returning only small subsets of the collection at one time.
159130For more information about the methods and classes mentioned in this guide,
160131see the following API Documentation:
161132
162- - `FindIterable <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html >`__
163- - `MongoIterable <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html >`__
164- - `MongoCursor <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCursor.html >`__
165- - `find() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#find( )>`__
133+ - `FindIterable <TODO:(DOCSP-29169) >`__
134+ - `MongoIterable <TODO:(DOCSP-29169) >`__
135+ - `MongoCursor <TODO:(DOCSP-29169) >`__
136+ - `find() <TODO:(DOCSP-29169 )>`__
0 commit comments