Skip to content

Commit 56dccd9

Browse files
authored
Update guides, examples, and README for 1.0 (#486)
1 parent 58e4018 commit 56dccd9

File tree

9 files changed

+195
-115
lines changed

9 files changed

+195
-115
lines changed

Examples/Docs/Sources/AsyncExamples/main.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private func causalConsistency() throws {
1717
// Start Causal Consistency Example 1
1818
let s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: true))
1919
let currentDate = Date()
20-
var dbOptions = DatabaseOptions(
20+
var dbOptions = MongoDatabaseOptions(
2121
readConcern: .majority,
2222
writeConcern: try .majority(wtimeoutMS: 1000)
2323
)
@@ -126,19 +126,19 @@ private func changeStreams() throws {
126126

127127
do {
128128
// Start Changestream Example 4
129-
let pipeline: [Document] = [
129+
let pipeline: [BSONDocument] = [
130130
["$match": ["fullDocument.username": "alice"]],
131131
["$addFields": ["newField": "this is an added field!"]]
132132
]
133133
let inventory = db.collection("inventory")
134134

135135
// Option 1: use next() to iterate
136-
let next = inventory.watch(pipeline, withEventType: Document.self).flatMap { changeStream in
136+
let next = inventory.watch(pipeline, withEventType: BSONDocument.self).flatMap { changeStream in
137137
changeStream.next()
138138
}
139139

140140
// Option 2: register a callback to execute for each document
141-
let result = inventory.watch(pipeline, withEventType: Document.self).flatMap { changeStream in
141+
let result = inventory.watch(pipeline, withEventType: BSONDocument.self).flatMap { changeStream in
142142
changeStream.forEach { event in
143143
// process event
144144
print(event)

Examples/Docs/Sources/SyncExamples/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private func causalConsistency() throws {
1111
// Start Causal Consistency Example 1
1212
let s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: true))
1313
let currentDate = Date()
14-
var dbOptions = DatabaseOptions(
14+
var dbOptions = MongoDatabaseOptions(
1515
readConcern: .majority,
1616
writeConcern: try .majority(wtimeoutMS: 1000)
1717
)
@@ -80,12 +80,12 @@ private func changeStreams() throws {
8080

8181
do {
8282
// Start Changestream Example 4
83-
let pipeline: [Document] = [
83+
let pipeline: [BSONDocument] = [
8484
["$match": ["fullDocument.username": "alice"]],
8585
["$addFields": ["newField": "this is an added field!"]]
8686
]
8787
let inventory = db.collection("inventory")
88-
let changeStream = try inventory.watch(pipeline, withEventType: Document.self)
88+
let changeStream = try inventory.watch(pipeline, withEventType: BSONDocument.self)
8989
let next = changeStream.next()
9090
// End Changestream Example 4
9191
}

Examples/KituraExample/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let package = Package(
55
name: "KituraExample",
66
dependencies: [
77
.package(url: "https://github.com/IBM-Swift/Kitura", .upToNextMajor(from: "2.9.1")),
8-
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0-rc0")),
8+
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0")),
99
.package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.14.0"))
1010
],
1111
targets: [

Examples/PerfectExample/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let package = Package(
55
name: "PerfectExample",
66
dependencies: [
77
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),
8-
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0-rc0")),
8+
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0")),
99
.package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.14.0"))
1010
],
1111
targets: [

Examples/VaporExample/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let package = Package(
99
dependencies: [
1010
// The driver depends on SwiftNIO 2 and therefore is only compatible with Vapor 4.
1111
.package(url: "https://github.com/vapor/vapor.git", from: "4.2.1"),
12-
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0-rc0"))
12+
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0"))
1313
],
1414
targets: [
1515
.target(name: "VaporExample", dependencies: [

Guides/BSON.md

Lines changed: 157 additions & 77 deletions
Large diffs are not rendered by default.

Guides/Change-Streams.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
1212
let client = try MongoClient(using: elg)
1313
let inventory = client.db("example").collection("inventory")
1414

15-
inventory.watch().flatMap { stream in // a `ChangeStream<ChangeStreamEvent<Document>>`
15+
inventory.watch().flatMap { stream in // a `ChangeStream<ChangeStreamEvent<BSONDocument>>`
1616
stream.forEach { event in
17-
// process `ChangeStreamEvent<Document>` here
17+
// process `ChangeStreamEvent<BSONDocument>` here
1818
}
1919
}.whenFailure { error in
2020
// handle error
@@ -80,9 +80,9 @@ let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
8080
let client = try MongoClient(using: elg)
8181
let db = client.db("example")
8282

83-
db.watch().flatMap { stream in // a `ChangeStream<ChangeStreamEvent<Document>>`
83+
db.watch().flatMap { stream in // a `ChangeStream<ChangeStreamEvent<BSONDocument>>`
8484
stream.forEach { event in
85-
// process `ChangeStreamEvent<Document>` here
85+
// process `ChangeStreamEvent<BSONDocument>` here
8686
}
8787
}.whenFailure { error in
8888
// handle error
@@ -98,9 +98,9 @@ Note: the types of the `fullDocument` property, as well as the return type of `C
9898
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
9999
let client = try MongoClient(using: elg)
100100

101-
client.watch().flatMap { stream in // a `ChangeStream<ChangeStreamEvent<Document>>`
101+
client.watch().flatMap { stream in // a `ChangeStream<ChangeStreamEvent<BSONDocument>>`
102102
stream.forEach { event in
103-
// process `ChangeStreamEvent<Document>` here
103+
// process `ChangeStreamEvent<BSONDocument>` here
104104
}
105105
}.whenFailure { error in
106106
// handle error
@@ -117,7 +117,7 @@ let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
117117
let client = try MongoClient(using: elg)
118118
let inventory = client.db("example").collection("inventory")
119119

120-
inventory.watch().flatMap { stream -> EventLoopFuture<ChangeStream<ChangeStreamEvent<Document>>> in
120+
inventory.watch().flatMap { stream -> EventLoopFuture<ChangeStream<ChangeStreamEvent<BSONDocument>>> in
121121
// read the first change event
122122
stream.next().flatMap { _ in
123123
// simulate an error by killing the stream
@@ -129,7 +129,7 @@ inventory.watch().flatMap { stream -> EventLoopFuture<ChangeStream<ChangeStreamE
129129
}
130130
}.flatMap { resumedStream in
131131
resumedStream.forEach { event in
132-
// process `ChangeStreamEvent<Document>` here
132+
// process `ChangeStreamEvent<BSONDocument>` here
133133
}
134134
}.whenFailure { error in
135135
// handle error
@@ -145,13 +145,13 @@ let client = try MongoClient(using: elg)
145145
let inventory = client.db("example").collection("inventory")
146146

147147
// Only include events where the changed document's username = "alice"
148-
let pipeline: [Document] = [
149-
["$match": ["fullDocument.username": "alice"] as Document]
148+
let pipeline: [BSONDocument] = [
149+
["$match": ["fullDocument.username": "alice"]]
150150
]
151151

152-
inventory.watch(pipeline).flatMap { stream in // a `ChangeStream<ChangeStreamEvent<Document>>`
152+
inventory.watch(pipeline).flatMap { stream in // a `ChangeStream<ChangeStreamEvent<BSONDocument>>`
153153
stream.forEach { event in
154-
// process `ChangeStreamEvent<Document>` here
154+
// process `ChangeStreamEvent<BSONDocument>` here
155155
}
156156
}.whenFailure { error in
157157
// handle error

Guides/TLS.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ macOS 10.13 (High Sierra) and newer support TLS 1.1+.
2727

2828
## Basic Configuration
2929

30-
To require that connections to MongoDB made by the driver use TLS/SSL, simply specify `tls: true` in the `ClientOptions` passed to a `MongoClient`'s initializer:
30+
To require that connections to MongoDB made by the driver use TLS/SSL, specify `tls: true` in the `MongoClientOptions` passed to a `MongoClient`'s initializer:
3131
```swift
32-
let client = try MongoClient("mongodb://example.com", using: elg, options: ClientOptions(tls: true))
32+
let client = try MongoClient("mongodb://example.com", using: elg, options: MongoClientOptions(tls: true))
3333
```
3434

3535
Alternatively, `tls=true` can be specified in the [MongoDB Connection String](https://docs.mongodb.com/manual/reference/connection-string/) passed to the initializer:
3636
```swift
3737
let client = try MongoClient("mongodb://example.com/?tls=true", using: elg)
3838
```
39-
**Note:** Specifying any `tls`-prefixed option in the connection string or `ClientOptions` will require all connections made by the driver to use TLS/SSL.
39+
**Note:** Specifying any `tls`-prefixed option in the connection string or `MongoClientOptions` will require all connections made by the driver to use TLS/SSL.
4040

4141
## Specifying a CA File
4242

4343
The driver can be configured to use a specific set of CA certificates. This is most often used with "self-signed" server certificates.
4444

45-
A path to a file with either a single or bundle of certificate authorities to be considered trusted when making a TLS connection can be specified via the `tlsCAFile` option on `ClientOptions`:
45+
A path to a file with either a single or bundle of certificate authorities to be considered trusted when making a TLS connection can be specified via the `tlsCAFile` option on `MongoClientOptions`:
4646
```swift
47-
let client = try MongoClient("mongodb://example.com", using: elg, options: ClientOptions(tlsCAFile: URL(string: "/path/to/ca.pem")))
47+
let client = try MongoClient("mongodb://example.com", using: elg, options: MongoClientOptions(tlsCAFile: URL(string: "/path/to/ca.pem")))
4848
```
4949

5050
Alternatively, the path can be specified via the `tlsCAFile` option in the [MongoDB Connection String](https://docs.mongodb.com/manual/reference/connection-string/) passed to the client's initializer:
@@ -55,16 +55,16 @@ let client = try MongoClient("mongodb://example.com/?tlsCAFile=\(caFile)", using
5555

5656
## Specifying a Client Certificate or Private Key File
5757

58-
The driver can be configured to present the client certificate file or the client private key file via the `tlsCertificateKeyFile` option on `ClientOptions`:
58+
The driver can be configured to present the client certificate file or the client private key file via the `tlsCertificateKeyFile` option on `MongoClientOptions`:
5959
```swift
60-
let client = try MongoClient("mongodb://example.com", using: elg, options: ClientOptions(tlsCertificateKeyFile: URL(string: "/path/to/cert.pem")))
60+
let client = try MongoClient("mongodb://example.com", using: elg, options: MongoClientOptions(tlsCertificateKeyFile: URL(string: "/path/to/cert.pem")))
6161
```
62-
If the private key is password protected, a password can be supplied via `tlsCertificateKeyFilePassword` on `ClientOptions`:
62+
If the private key is password protected, a password can be supplied via `tlsCertificateKeyFilePassword` on `MongoClientOptions`:
6363
```swift
6464
let client = try MongoClient(
6565
"mongodb://example.com",
6666
using: elg,
67-
options: ClientOptions(tlsCertificateKeyFile: URL(string: "/path/to/cert.pem"), tlsCertificateKeyFilePassword: <password>)
67+
options: MongoClientOptions(tlsCertificateKeyFile: URL(string: "/path/to/cert.pem"), tlsCertificateKeyFilePassword: <password>)
6868
)
6969
```
7070

@@ -73,7 +73,7 @@ Alternatively, these options can be set via the `tlsCertificateKeyFile` and `tls
7373
let certificatePath = "/path/to/cert.pem".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
7474
let password = "not a secure password".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
7575
let client = try MongoClient(
76-
"mongodb://example.com/?tlsCertificateKeyFile=\(certificatePath)&tlsCertificateKeyFilePassword=\(password)"
76+
"mongodb://example.com/?tlsCertificateKeyFile=\(certificatePath)&tlsCertificateKeyFilePassword=\(password)",
7777
using: elg
7878
)
7979
```

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,23 @@ Note: we have included the client `connectionString` parameter for clarity, but
119119
### Create and Insert a Document
120120
**Async**:
121121
```swift
122-
let doc: Document = ["_id": 100, "a": 1, "b": 2, "c": 3]
122+
let doc: BSONDocument = ["_id": 100, "a": 1, "b": 2, "c": 3]
123123
collection.insertOne(doc).whenSuccess { result in
124124
print(result?.insertedId ?? "") // prints `.int64(100)`
125125
}
126126
```
127127

128128
**Sync**:
129129
```swift
130-
let doc: Document = ["_id": 100, "a": 1, "b": 2, "c": 3]
130+
let doc: BSONDocument = ["_id": 100, "a": 1, "b": 2, "c": 3]
131131
let result = try collection.insertOne(doc)
132132
print(result?.insertedId ?? "") // prints `.int64(100)`
133133
```
134134

135135
### Find Documents
136136
**Async**:
137137
```swift
138-
let query: Document = ["a": 1]
138+
let query: BSONDocument = ["a": 1]
139139
let result = collection.find(query).flatMap { cursor in
140140
cursor.forEach { doc in
141141
print(doc)
@@ -145,7 +145,7 @@ let result = collection.find(query).flatMap { cursor in
145145

146146
**Sync**:
147147
```swift
148-
let query: Document = ["a": 1]
148+
let query: BSONDocument = ["a": 1]
149149
let documents = try collection.find(query)
150150
for d in documents {
151151
print(try d.get())
@@ -154,7 +154,7 @@ for d in documents {
154154

155155
### Work With and Modify Documents
156156
```swift
157-
var doc: Document = ["a": 1, "b": 2, "c": 3]
157+
var doc: BSONDocument = ["a": 1, "b": 2, "c": 3]
158158

159159
print(doc) // prints `{"a" : 1, "b" : 2, "c" : 3}`
160160
print(doc["a"] ?? "") // prints `.int64(1)`
@@ -182,7 +182,7 @@ let doubled = doc.map { elem -> Int in
182182
print(doubled) // prints `[2, 4, 6, 8]`
183183
```
184184

185-
Note that `Document` conforms to `Collection`, so useful methods from
185+
Note that `BSONDocument` conforms to `Collection`, so useful methods from
186186
[`Sequence`](https://developer.apple.com/documentation/swift/sequence) and
187187
[`Collection`](https://developer.apple.com/documentation/swift/collection) are
188188
all available. However, runtime guarantees are not yet met for many of these

0 commit comments

Comments
 (0)