Skip to content

Commit 0fd0cbc

Browse files
authored
SWIFT-1034 Add sorted find example to README (#605)
1 parent a9a13a6 commit 0fd0cbc

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ print(result?.insertedID ?? "") // prints `.int64(100)`
137137
**Async**:
138138
```swift
139139
let query: BSONDocument = ["a": 1]
140-
let result = collection.find(query).flatMap { cursor in
140+
// The `sort` option specifies the order in which results are returned
141+
// via the cursor. In this case, `["_id": -1]` indicates that the documents will
142+
// be returned in descending order according to the `_id` field.
143+
let options = FindOptions(sort: ["_id": -1])
144+
let result = collection.find(query, options: options).flatMap { cursor in
141145
cursor.forEach { doc in
142146
print(doc)
143147
}
@@ -147,7 +151,11 @@ let result = collection.find(query).flatMap { cursor in
147151
**Sync**:
148152
```swift
149153
let query: BSONDocument = ["a": 1]
150-
let documents = try collection.find(query)
154+
// The `sort` option specifies the order in which results are returned
155+
// via the cursor. In this case, `["_id": -1]` indicates that the documents will
156+
// be returned in descending order according to the `_id` field.
157+
let options = FindOptions(sort: ["_id": -1])
158+
let documents = try collection.find(query, options: options)
151159
for d in documents {
152160
print(try d.get())
153161
}

0 commit comments

Comments
 (0)