Skip to content

Commit b880706

Browse files
authored
SWIFT-642 Update examples to use the new BSON API (#340)
1 parent bc95b1c commit b880706

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

Examples/Docs/Sources/DocsExamples/main.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import MongoSwift
66
/// Examples used for the MongoDB documentation on Causal Consistency.
77
/// - SeeAlso: https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#examples
88
private func causalConsistency() throws {
9-
let client1 = try MongoClient()
9+
let client1 = try SyncMongoClient()
1010

1111
// Start Causal Consistency Example 1
1212
let s1 = try client1.startSession(options: ClientSessionOptions(causalConsistency: true))
@@ -17,14 +17,14 @@ private func causalConsistency() throws {
1717
)
1818
let items = client1.db("test", options: dbOptions).collection("items")
1919
try items.updateOne(
20-
filter: ["sku": "111", "end": BSONNull()],
21-
update: ["$set": ["end": currentDate] as Document],
20+
filter: ["sku": "111", "end": .null],
21+
update: ["$set": ["end": .datetime(currentDate)]],
2222
session: s1
2323
)
24-
try items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": currentDate], session: s1)
24+
try items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": .datetime(currentDate)], session: s1)
2525
// End Causal Consistency Example 1
2626

27-
let client2 = try MongoClient()
27+
let client2 = try SyncMongoClient()
2828

2929
// Start Causal Consistency Example 2
3030
try client2.withSession(options: ClientSessionOptions(causalConsistency: true)) { s2 in
@@ -34,7 +34,7 @@ private func causalConsistency() throws {
3434

3535
dbOptions.readPreference = ReadPreference(.secondary)
3636
let items2 = client2.db("test", options: dbOptions).collection("items")
37-
for item in try items2.find(["end": BSONNull()], session: s2) {
37+
for item in try items2.find(["end": .null], session: s2) {
3838
print(item)
3939
}
4040
}
@@ -44,7 +44,7 @@ private func causalConsistency() throws {
4444
/// Examples used for the MongoDB documentation on Change Streams.
4545
/// - SeeAlso: https://docs.mongodb.com/manual/changeStreams/
4646
private func changeStreams() throws {
47-
let client = try MongoClient()
47+
let client = try SyncMongoClient()
4848
let db = client.db("example")
4949

5050
// The following examples assume that you have connected to a MongoDB replica set and have
@@ -81,8 +81,8 @@ private func changeStreams() throws {
8181
do {
8282
// Start Changestream Example 4
8383
let pipeline: [Document] = [
84-
["$match": ["fullDocument.username": "alice"] as Document],
85-
["$addFields": ["newField": "this is an added field!"] as Document]
84+
["$match": ["fullDocument.username": "alice"]],
85+
["$addFields": ["newField": "this is an added field!"]]
8686
]
8787
let inventory = db.collection("inventory")
8888
let cursor = try inventory.watch(pipeline, withEventType: Document.self)

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Note: we have included the client `connectionString` parameter for clarity, but
9191
```swift
9292
let doc: Document = ["_id": 100, "a": 1, "b": 2, "c": 3]
9393
let result = try collection.insertOne(doc)
94-
print(result?.insertedId ?? "") // prints `100`
94+
print(result?.insertedId ?? "") // prints `.int64(100)`
9595
```
9696

9797
### Find Documents
@@ -108,27 +108,27 @@ for d in documents {
108108
var doc: Document = ["a": 1, "b": 2, "c": 3]
109109

110110
print(doc) // prints `{"a" : 1, "b" : 2, "c" : 3}`
111-
print(doc["a"] ?? "") // prints `1`
111+
print(doc["a"] ?? "") // prints `.int64(1)`
112112

113113
// Set a new value
114114
doc["d"] = 4
115115
print(doc) // prints `{"a" : 1, "b" : 2, "c" : 3, "d" : 4}`
116116

117117
// Using functional methods like map, filter:
118118
let evensDoc = doc.filter { elem in
119-
guard let value = elem.value as? Int else {
119+
guard let value = elem.value.asInt() else {
120120
return false
121121
}
122122
return value % 2 == 0
123123
}
124124
print(evensDoc) // prints `{ "b" : 2, "d" : 4 }`
125125

126126
let doubled = doc.map { elem -> Int in
127-
guard let value = elem.value as? Int else {
127+
guard case let value = .int64(value) else {
128128
return 0
129129
}
130130

131-
return value * 2
131+
return Int(value * 2)
132132
}
133133
print(doubled) // prints `[2, 4, 6, 8]`
134134
```

0 commit comments

Comments
 (0)