Skip to content

Commit 12fc74e

Browse files
authored
Final changes for 1.0.0-rc0 (#417)
1 parent b5229ce commit 12fc74e

File tree

384 files changed

+410
-240872
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

384 files changed

+410
-240872
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ What does this command give you?
3131
```
3232
$ cat Package.resolved # Applies if you are using Swift package manager
3333
```
34-
*or*
35-
```
36-
$ cat Podfile.lock # Applies if you are using Cocoapods
37-
```
3834

3935

4036
### What is the version(s) of `mongod` that you are running with the driver?
@@ -53,28 +49,6 @@ or, running this in a MongoDB shell connected to the relevant node(s):
5349
How is your MongoDB deployment configured?
5450

5551

56-
### How did you install `libmongoc` and `libbson` on your system
57-
58-
Did you use `brew`? Did you install them manually? etc.
59-
60-
61-
### Version of `libmongoc` and `libbson`
62-
63-
What does this command give you?
64-
```
65-
$ brew list --versions mongo-c-driver # Applies if you installed via brew
66-
```
67-
*or*
68-
```
69-
$ apt list --installed | grep -E '(libmongoc|libbson)' # Applies if you installed via apt
70-
```
71-
*or*
72-
```
73-
$ pkg-config --modversion libmongoc-1.0 # Applies if you use pkg-config
74-
$ pkg-config --modversion libbson-1.0
75-
```
76-
77-
7852
## What is the problem?
7953

8054
**BE SPECIFIC**:

.jazzy.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

Examples/.swiftlint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
disabled_rules:
2+
- explicit_acl
3+
- nesting

Examples/Docker/Dockerfile

Lines changed: 0 additions & 14 deletions
This file was deleted.

Examples/Docker/Dockerfile.vapor

Lines changed: 0 additions & 65 deletions
This file was deleted.

Examples/Docker/README.md

Lines changed: 0 additions & 34 deletions
This file was deleted.

Examples/Docs/Package.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
// swift-tools-version:4.2
1+
// swift-tools-version:5.0
22
import PackageDescription
33

44
let package = Package(
55
name: "DocsExamples",
66
dependencies: [
7-
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0"))
7+
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "1.0.0")),
8+
.package(url: "https://github.com/apple/swift-nio", .upToNextMajor(from: "2.0.0"))
89
],
910
targets: [
10-
.target(name: "DocsExamples", dependencies: ["MongoSwift"])
11+
.target(name: "SyncExamples", dependencies: ["MongoSwiftSync"]),
12+
.target(name: "AsyncExamples", dependencies: ["MongoSwift", "NIO"])
1113
]
1214
)
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import Foundation
2+
import MongoSwift
3+
import NIO
4+
5+
// swiftlint:disable force_unwrapping
6+
7+
/// Examples used for the MongoDB documentation on Causal Consistency.
8+
/// - SeeAlso: https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#examples
9+
private func causalConsistency() throws {
10+
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
11+
let client1 = try MongoClient(using: elg)
12+
defer {
13+
client1.syncShutdown()
14+
try? elg.syncShutdownGracefully()
15+
}
16+
17+
// Start Causal Consistency Example 1
18+
let s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: true))
19+
let currentDate = Date()
20+
var dbOptions = DatabaseOptions(
21+
readConcern: ReadConcern(.majority),
22+
writeConcern: try WriteConcern(w: .majority, wtimeoutMS: 1000)
23+
)
24+
let items = client1.db("test", options: dbOptions).collection("items")
25+
let result1 = items.updateOne(
26+
filter: ["sku": "111", "end": .null],
27+
update: ["$set": ["end": .datetime(currentDate)]],
28+
session: s1
29+
).flatMap { _ in
30+
items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": .datetime(currentDate)], session: s1)
31+
}
32+
// End Causal Consistency Example 1
33+
34+
let client2 = try MongoClient(using: elg)
35+
36+
// Start Causal Consistency Example 2
37+
let options = ClientSessionOptions(causalConsistency: true)
38+
let result2: EventLoopFuture<Void> = client2.withSession(options: options) { s2 in
39+
// The cluster and operation times are guaranteed to be non-nil since we already used s1 for operations above.
40+
s2.advanceClusterTime(to: s1.clusterTime!)
41+
s2.advanceOperationTime(to: s1.operationTime!)
42+
43+
dbOptions.readPreference = ReadPreference(.secondary)
44+
let items2 = client2.db("test", options: dbOptions).collection("items")
45+
46+
return items2.find(["end": .null], session: s2).flatMap { cursor in
47+
cursor.forEach { item in
48+
print(item)
49+
}
50+
}
51+
}
52+
// End Causal Consistency Example 2
53+
}
54+
55+
/// Examples used for the MongoDB documentation on Change Streams.
56+
/// - SeeAlso: https://docs.mongodb.com/manual/changeStreams/
57+
private func changeStreams() throws {
58+
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
59+
let client = try MongoClient(using: elg)
60+
let db = client.db("example")
61+
62+
// The following examples assume that you have connected to a MongoDB replica set and have
63+
// accessed a database that contains an inventory collection.
64+
65+
do {
66+
// Start Changestream Example 1
67+
let inventory = db.collection("inventory")
68+
69+
// Option 1: retrieve next document via next()
70+
let next = inventory.watch().flatMap { cursor in
71+
cursor.next()
72+
}
73+
74+
// Option 2: register a callback to execute for each document
75+
let result = inventory.watch().flatMap { cursor in
76+
cursor.forEach { event in
77+
// process event
78+
print(event)
79+
}
80+
}
81+
// End Changestream Example 1
82+
}
83+
84+
do {
85+
// Start Changestream Example 2
86+
let inventory = db.collection("inventory")
87+
88+
// Option 1: use next() to iterate
89+
let next = inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))
90+
.flatMap { changeStream in
91+
changeStream.next()
92+
}
93+
94+
// Option 2: register a callback to execute for each document
95+
let result = inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))
96+
.flatMap { changeStream in
97+
changeStream.forEach { event in
98+
// process event
99+
print(event)
100+
}
101+
}
102+
// End Changestream Example 2
103+
}
104+
105+
do {
106+
// Start Changestream Example 3
107+
let inventory = db.collection("inventory")
108+
109+
inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))
110+
.flatMap { changeStream in
111+
changeStream.next().map { _ in
112+
changeStream.resumeToken
113+
}.always { _ in
114+
_ = changeStream.kill()
115+
}
116+
}.flatMap { resumeToken in
117+
inventory.watch(options: ChangeStreamOptions(resumeAfter: resumeToken)).flatMap { newStream in
118+
newStream.forEach { event in
119+
// process event
120+
print(event)
121+
}
122+
}
123+
}
124+
// End Changestream Example 3
125+
}
126+
127+
do {
128+
// Start Changestream Example 4
129+
let pipeline: [Document] = [
130+
["$match": ["fullDocument.username": "alice"]],
131+
["$addFields": ["newField": "this is an added field!"]]
132+
]
133+
let inventory = db.collection("inventory")
134+
135+
// Option 1: use next() to iterate
136+
let next = inventory.watch(pipeline, withEventType: Document.self).flatMap { changeStream in
137+
changeStream.next()
138+
}
139+
140+
// Option 2: register a callback to execute for each document
141+
let result = inventory.watch(pipeline, withEventType: Document.self).flatMap { changeStream in
142+
changeStream.forEach { event in
143+
// process event
144+
print(event)
145+
}
146+
}
147+
// End Changestream Example 4
148+
}
149+
}

0 commit comments

Comments
 (0)