From f387f5bdbf33518f26ac1acb678172a657ca1035 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Tue, 15 Oct 2019 15:59:30 -0400 Subject: [PATCH 1/4] update examples to use new BSON api --- Examples/Docs/Sources/DocsExamples/main.swift | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Examples/Docs/Sources/DocsExamples/main.swift b/Examples/Docs/Sources/DocsExamples/main.swift index 7445865ef..b4d30c901 100644 --- a/Examples/Docs/Sources/DocsExamples/main.swift +++ b/Examples/Docs/Sources/DocsExamples/main.swift @@ -6,7 +6,7 @@ import MongoSwift /// Examples used for the MongoDB documentation on Causal Consistency. /// - SeeAlso: https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#examples private func causalConsistency() throws { - let client1 = try MongoClient() + let client1 = try SyncMongoClient() // Start Causal Consistency Example 1 let s1 = try client1.startSession(options: ClientSessionOptions(causalConsistency: true)) @@ -16,12 +16,10 @@ private func causalConsistency() throws { writeConcern: try WriteConcern(w: .majority, wtimeoutMS: 1000) ) let items = client1.db("test", options: dbOptions).collection("items") - try items.updateOne( - filter: ["sku": "111", "end": BSONNull()], - update: ["$set": ["end": currentDate] as Document], - session: s1 - ) - try items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": currentDate], session: s1) + try items.updateOne(filter: ["sku": "111", "end": .null], + update: ["$set": ["end": .datetime(currentDate)]], + session: s1) + try items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": .datetime(currentDate)], session: s1) // End Causal Consistency Example 1 let client2 = try MongoClient() @@ -34,7 +32,7 @@ private func causalConsistency() throws { dbOptions.readPreference = ReadPreference(.secondary) let items2 = client2.db("test", options: dbOptions).collection("items") - for item in try items2.find(["end": BSONNull()], session: s2) { + for item in try items2.find(["end": .null], session: s2) { print(item) } } @@ -81,8 +79,8 @@ private func changeStreams() throws { do { // Start Changestream Example 4 let pipeline: [Document] = [ - ["$match": ["fullDocument.username": "alice"] as Document], - ["$addFields": ["newField": "this is an added field!"] as Document] + ["$match": ["fullDocument.username": "alice"]], + ["$addFields": ["newField": "this is an added field!"]] ] let inventory = db.collection("inventory") let cursor = try inventory.watch(pipeline, withEventType: Document.self) From f90e81ec4b1e1c04f13ffe624143e95c37f01a58 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Tue, 22 Oct 2019 18:10:49 -0400 Subject: [PATCH 2/4] update readme --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 30ffc9469..70311f8fc 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ Note: we have included the client `connectionString` parameter for clarity, but ```swift let doc: Document = ["_id": 100, "a": 1, "b": 2, "c": 3] let result = try collection.insertOne(doc) -print(result?.insertedId ?? "") // prints `100` +print(result?.insertedId ?? "") // prints `.int64(100)` ``` ### Find Documents @@ -108,7 +108,7 @@ for d in documents { var doc: Document = ["a": 1, "b": 2, "c": 3] print(doc) // prints `{"a" : 1, "b" : 2, "c" : 3}` -print(doc["a"] ?? "") // prints `1` +print(doc["a"] ?? "") // prints `.int64(1)` // Set a new value doc["d"] = 4 @@ -116,7 +116,7 @@ print(doc) // prints `{"a" : 1, "b" : 2, "c" : 3, "d" : 4}` // Using functional methods like map, filter: let evensDoc = doc.filter { elem in - guard let value = elem.value as? Int else { + guard let value = elem.value.asInt() else { return false } return value % 2 == 0 @@ -124,11 +124,11 @@ let evensDoc = doc.filter { elem in print(evensDoc) // prints `{ "b" : 2, "d" : 4 }` let doubled = doc.map { elem -> Int in - guard let value = elem.value as? Int else { + guard case let value = .int64(value) else { return 0 } - return value * 2 + return Int(value * 2) } print(doubled) // prints `[2, 4, 6, 8]` ``` From ce207b8c675f57ea2bfaee5d358747ff5cc7a64a Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Tue, 22 Oct 2019 23:39:15 -0400 Subject: [PATCH 3/4] run formatter --- Examples/Docs/Sources/DocsExamples/main.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Examples/Docs/Sources/DocsExamples/main.swift b/Examples/Docs/Sources/DocsExamples/main.swift index b4d30c901..cae4acd2f 100644 --- a/Examples/Docs/Sources/DocsExamples/main.swift +++ b/Examples/Docs/Sources/DocsExamples/main.swift @@ -16,9 +16,11 @@ private func causalConsistency() throws { writeConcern: try WriteConcern(w: .majority, wtimeoutMS: 1000) ) let items = client1.db("test", options: dbOptions).collection("items") - try items.updateOne(filter: ["sku": "111", "end": .null], - update: ["$set": ["end": .datetime(currentDate)]], - session: s1) + try items.updateOne( + filter: ["sku": "111", "end": .null], + update: ["$set": ["end": .datetime(currentDate)]], + session: s1 + ) try items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": .datetime(currentDate)], session: s1) // End Causal Consistency Example 1 From d32fc8eee9f4b77ff3ddfb3520e78ac8da792ec8 Mon Sep 17 00:00:00 2001 From: Patrick Freed Date: Wed, 23 Oct 2019 12:03:41 -0400 Subject: [PATCH 4/4] fix compilation issues --- Examples/Docs/Sources/DocsExamples/main.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Examples/Docs/Sources/DocsExamples/main.swift b/Examples/Docs/Sources/DocsExamples/main.swift index cae4acd2f..24042bad8 100644 --- a/Examples/Docs/Sources/DocsExamples/main.swift +++ b/Examples/Docs/Sources/DocsExamples/main.swift @@ -24,7 +24,7 @@ private func causalConsistency() throws { try items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": .datetime(currentDate)], session: s1) // End Causal Consistency Example 1 - let client2 = try MongoClient() + let client2 = try SyncMongoClient() // Start Causal Consistency Example 2 try client2.withSession(options: ClientSessionOptions(causalConsistency: true)) { s2 in @@ -44,7 +44,7 @@ private func causalConsistency() throws { /// Examples used for the MongoDB documentation on Change Streams. /// - SeeAlso: https://docs.mongodb.com/manual/changeStreams/ private func changeStreams() throws { - let client = try MongoClient() + let client = try SyncMongoClient() let db = client.db("example") // The following examples assume that you have connected to a MongoDB replica set and have