Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Examples/Docs/Sources/AsyncExamples/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private func causalConsistency() throws {
// Start Causal Consistency Example 1
let s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: true))
let currentDate = Date()
var dbOptions = DatabaseOptions(
var dbOptions = MongoDatabaseOptions(
readConcern: .majority,
writeConcern: try .majority(wtimeoutMS: 1000)
)
Expand Down Expand Up @@ -126,19 +126,19 @@ private func changeStreams() throws {

do {
// Start Changestream Example 4
let pipeline: [Document] = [
let pipeline: [BSONDocument] = [
["$match": ["fullDocument.username": "alice"]],
["$addFields": ["newField": "this is an added field!"]]
]
let inventory = db.collection("inventory")

// Option 1: use next() to iterate
let next = inventory.watch(pipeline, withEventType: Document.self).flatMap { changeStream in
let next = inventory.watch(pipeline, withEventType: BSONDocument.self).flatMap { changeStream in
changeStream.next()
}

// Option 2: register a callback to execute for each document
let result = inventory.watch(pipeline, withEventType: Document.self).flatMap { changeStream in
let result = inventory.watch(pipeline, withEventType: BSONDocument.self).flatMap { changeStream in
changeStream.forEach { event in
// process event
print(event)
Expand Down
6 changes: 3 additions & 3 deletions Examples/Docs/Sources/SyncExamples/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ private func causalConsistency() throws {
// Start Causal Consistency Example 1
let s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: true))
let currentDate = Date()
var dbOptions = DatabaseOptions(
var dbOptions = MongoDatabaseOptions(
readConcern: .majority,
writeConcern: try .majority(wtimeoutMS: 1000)
)
Expand Down Expand Up @@ -80,12 +80,12 @@ private func changeStreams() throws {

do {
// Start Changestream Example 4
let pipeline: [Document] = [
let pipeline: [BSONDocument] = [
["$match": ["fullDocument.username": "alice"]],
["$addFields": ["newField": "this is an added field!"]]
]
let inventory = db.collection("inventory")
let changeStream = try inventory.watch(pipeline, withEventType: Document.self)
let changeStream = try inventory.watch(pipeline, withEventType: BSONDocument.self)
let next = changeStream.next()
// End Changestream Example 4
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/KituraExample/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let package = Package(
name: "KituraExample",
dependencies: [
.package(url: "https://github.com/IBM-Swift/Kitura", .upToNextMajor(from: "2.9.1")),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0-rc0")),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.14.0"))
],
targets: [
Expand Down
2 changes: 1 addition & 1 deletion Examples/PerfectExample/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let package = Package(
name: "PerfectExample",
dependencies: [
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0-rc0")),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.14.0"))
],
targets: [
Expand Down
2 changes: 1 addition & 1 deletion Examples/VaporExample/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let package = Package(
dependencies: [
// The driver depends on SwiftNIO 2 and therefore is only compatible with Vapor 4.
.package(url: "https://github.com/vapor/vapor.git", from: "4.2.1"),
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0-rc0"))
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(name: "VaporExample", dependencies: [
Expand Down
234 changes: 157 additions & 77 deletions Guides/BSON.md

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions Guides/Change-Streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
let client = try MongoClient(using: elg)
let inventory = client.db("example").collection("inventory")

inventory.watch().flatMap { stream in // a `ChangeStream<ChangeStreamEvent<Document>>`
inventory.watch().flatMap { stream in // a `ChangeStream<ChangeStreamEvent<BSONDocument>>`
stream.forEach { event in
// process `ChangeStreamEvent<Document>` here
// process `ChangeStreamEvent<BSONDocument>` here
}
}.whenFailure { error in
// handle error
Expand Down Expand Up @@ -80,9 +80,9 @@ let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
let client = try MongoClient(using: elg)
let db = client.db("example")

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

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

inventory.watch().flatMap { stream -> EventLoopFuture<ChangeStream<ChangeStreamEvent<Document>>> in
inventory.watch().flatMap { stream -> EventLoopFuture<ChangeStream<ChangeStreamEvent<BSONDocument>>> in
// read the first change event
stream.next().flatMap { _ in
// simulate an error by killing the stream
Expand All @@ -129,7 +129,7 @@ inventory.watch().flatMap { stream -> EventLoopFuture<ChangeStream<ChangeStreamE
}
}.flatMap { resumedStream in
resumedStream.forEach { event in
// process `ChangeStreamEvent<Document>` here
// process `ChangeStreamEvent<BSONDocument>` here
}
}.whenFailure { error in
// handle error
Expand All @@ -145,13 +145,13 @@ let client = try MongoClient(using: elg)
let inventory = client.db("example").collection("inventory")

// Only include events where the changed document's username = "alice"
let pipeline: [Document] = [
["$match": ["fullDocument.username": "alice"] as Document]
let pipeline: [BSONDocument] = [
["$match": ["fullDocument.username": "alice"]]
]

inventory.watch(pipeline).flatMap { stream in // a `ChangeStream<ChangeStreamEvent<Document>>`
inventory.watch(pipeline).flatMap { stream in // a `ChangeStream<ChangeStreamEvent<BSONDocument>>`
stream.forEach { event in
// process `ChangeStreamEvent<Document>` here
// process `ChangeStreamEvent<BSONDocument>` here
}
}.whenFailure { error in
// handle error
Expand Down
20 changes: 10 additions & 10 deletions Guides/TLS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ macOS 10.13 (High Sierra) and newer support TLS 1.1+.

## Basic Configuration

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:
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:
```swift
let client = try MongoClient("mongodb://example.com", using: elg, options: ClientOptions(tls: true))
let client = try MongoClient("mongodb://example.com", using: elg, options: MongoClientOptions(tls: true))
```

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

## Specifying a CA File

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

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`:
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`:
```swift
let client = try MongoClient("mongodb://example.com", using: elg, options: ClientOptions(tlsCAFile: URL(string: "/path/to/ca.pem")))
let client = try MongoClient("mongodb://example.com", using: elg, options: MongoClientOptions(tlsCAFile: URL(string: "/path/to/ca.pem")))
```

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:
Expand All @@ -55,16 +55,16 @@ let client = try MongoClient("mongodb://example.com/?tlsCAFile=\(caFile)", using

## Specifying a Client Certificate or Private Key File

The driver can be configured to present the client certificate file or the client private key file via the `tlsCertificateKeyFile` option on `ClientOptions`:
The driver can be configured to present the client certificate file or the client private key file via the `tlsCertificateKeyFile` option on `MongoClientOptions`:
```swift
let client = try MongoClient("mongodb://example.com", using: elg, options: ClientOptions(tlsCertificateKeyFile: URL(string: "/path/to/cert.pem")))
let client = try MongoClient("mongodb://example.com", using: elg, options: MongoClientOptions(tlsCertificateKeyFile: URL(string: "/path/to/cert.pem")))
```
If the private key is password protected, a password can be supplied via `tlsCertificateKeyFilePassword` on `ClientOptions`:
If the private key is password protected, a password can be supplied via `tlsCertificateKeyFilePassword` on `MongoClientOptions`:
```swift
let client = try MongoClient(
"mongodb://example.com",
using: elg,
options: ClientOptions(tlsCertificateKeyFile: URL(string: "/path/to/cert.pem"), tlsCertificateKeyFilePassword: <password>)
options: MongoClientOptions(tlsCertificateKeyFile: URL(string: "/path/to/cert.pem"), tlsCertificateKeyFilePassword: <password>)
)
```

Expand All @@ -73,7 +73,7 @@ Alternatively, these options can be set via the `tlsCertificateKeyFile` and `tls
let certificatePath = "/path/to/cert.pem".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
let password = "not a secure password".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
let client = try MongoClient(
"mongodb://example.com/?tlsCertificateKeyFile=\(certificatePath)&tlsCertificateKeyFilePassword=\(password)"
"mongodb://example.com/?tlsCertificateKeyFile=\(certificatePath)&tlsCertificateKeyFilePassword=\(password)",
using: elg
)
```
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,23 @@ Note: we have included the client `connectionString` parameter for clarity, but
### Create and Insert a Document
**Async**:
```swift
let doc: Document = ["_id": 100, "a": 1, "b": 2, "c": 3]
let doc: BSONDocument = ["_id": 100, "a": 1, "b": 2, "c": 3]
collection.insertOne(doc).whenSuccess { result in
print(result?.insertedId ?? "") // prints `.int64(100)`
}
```

**Sync**:
```swift
let doc: Document = ["_id": 100, "a": 1, "b": 2, "c": 3]
let doc: BSONDocument = ["_id": 100, "a": 1, "b": 2, "c": 3]
let result = try collection.insertOne(doc)
print(result?.insertedId ?? "") // prints `.int64(100)`
```

### Find Documents
**Async**:
```swift
let query: Document = ["a": 1]
let query: BSONDocument = ["a": 1]
let result = collection.find(query).flatMap { cursor in
cursor.forEach { doc in
print(doc)
Expand All @@ -145,7 +145,7 @@ let result = collection.find(query).flatMap { cursor in

**Sync**:
```swift
let query: Document = ["a": 1]
let query: BSONDocument = ["a": 1]
let documents = try collection.find(query)
for d in documents {
print(try d.get())
Expand All @@ -154,7 +154,7 @@ for d in documents {

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

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

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