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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
26 changes: 0 additions & 26 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ What does this command give you?
```
$ cat Package.resolved # Applies if you are using Swift package manager
```
*or*
```
$ cat Podfile.lock # Applies if you are using Cocoapods
```


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


### How did you install `libmongoc` and `libbson` on your system

Did you use `brew`? Did you install them manually? etc.


### Version of `libmongoc` and `libbson`

What does this command give you?
```
$ brew list --versions mongo-c-driver # Applies if you installed via brew
```
*or*
```
$ apt list --installed | grep -E '(libmongoc|libbson)' # Applies if you installed via apt
```
*or*
```
$ pkg-config --modversion libmongoc-1.0 # Applies if you use pkg-config
$ pkg-config --modversion libbson-1.0
```


## What is the problem?

**BE SPECIFIC**:
Expand Down
5 changes: 0 additions & 5 deletions .jazzy.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions Examples/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
disabled_rules:
- explicit_acl
- nesting
14 changes: 0 additions & 14 deletions Examples/Docker/Dockerfile

This file was deleted.

65 changes: 0 additions & 65 deletions Examples/Docker/Dockerfile.vapor

This file was deleted.

34 changes: 0 additions & 34 deletions Examples/Docker/README.md

This file was deleted.

8 changes: 5 additions & 3 deletions Examples/Docs/Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// swift-tools-version:4.2
// swift-tools-version:5.0
import PackageDescription

let package = Package(
name: "DocsExamples",
dependencies: [
.package(url: "https://github.com/mongodb/mongo-swift-driver", .upToNextMajor(from: "0.1.0"))
.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.0.0"))
],
targets: [
.target(name: "DocsExamples", dependencies: ["MongoSwift"])
.target(name: "SyncExamples", dependencies: ["MongoSwiftSync"]),
.target(name: "AsyncExamples", dependencies: ["MongoSwift", "NIO"])
]
)
149 changes: 149 additions & 0 deletions Examples/Docs/Sources/AsyncExamples/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import Foundation
import MongoSwift
import NIO

// swiftlint:disable force_unwrapping

/// 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 elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let client1 = try MongoClient(using: elg)
defer {
client1.syncShutdown()
try? elg.syncShutdownGracefully()
}

// Start Causal Consistency Example 1
let s1 = client1.startSession(options: ClientSessionOptions(causalConsistency: true))
let currentDate = Date()
var dbOptions = DatabaseOptions(
readConcern: ReadConcern(.majority),
writeConcern: try WriteConcern(w: .majority, wtimeoutMS: 1000)
)
let items = client1.db("test", options: dbOptions).collection("items")
let result1 = items.updateOne(
filter: ["sku": "111", "end": .null],
update: ["$set": ["end": .datetime(currentDate)]],
session: s1
).flatMap { _ in
items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": .datetime(currentDate)], session: s1)
}
// End Causal Consistency Example 1

let client2 = try MongoClient(using: elg)

// Start Causal Consistency Example 2
let options = ClientSessionOptions(causalConsistency: true)
let result2: EventLoopFuture<Void> = client2.withSession(options: options) { s2 in
// The cluster and operation times are guaranteed to be non-nil since we already used s1 for operations above.
s2.advanceClusterTime(to: s1.clusterTime!)
s2.advanceOperationTime(to: s1.operationTime!)

dbOptions.readPreference = ReadPreference(.secondary)
let items2 = client2.db("test", options: dbOptions).collection("items")

return items2.find(["end": .null], session: s2).flatMap { cursor in
cursor.forEach { item in
print(item)
}
}
}
// End Causal Consistency Example 2
}

/// Examples used for the MongoDB documentation on Change Streams.
/// - SeeAlso: https://docs.mongodb.com/manual/changeStreams/
private func changeStreams() throws {
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let client = try MongoClient(using: elg)
let db = client.db("example")

// The following examples assume that you have connected to a MongoDB replica set and have
// accessed a database that contains an inventory collection.

do {
// Start Changestream Example 1
let inventory = db.collection("inventory")

// Option 1: retrieve next document via next()
let next = inventory.watch().flatMap { cursor in
cursor.next()
}

// Option 2: register a callback to execute for each document
let result = inventory.watch().flatMap { cursor in
cursor.forEach { event in
// process event
print(event)
}
}
// End Changestream Example 1
}

do {
// Start Changestream Example 2
let inventory = db.collection("inventory")

// Option 1: use next() to iterate
let next = inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))
.flatMap { changeStream in
changeStream.next()
}

// Option 2: register a callback to execute for each document
let result = inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))
.flatMap { changeStream in
changeStream.forEach { event in
// process event
print(event)
}
}
// End Changestream Example 2
}

do {
// Start Changestream Example 3
let inventory = db.collection("inventory")

inventory.watch(options: ChangeStreamOptions(fullDocument: .updateLookup))
.flatMap { changeStream in
changeStream.next().map { _ in
changeStream.resumeToken
}.always { _ in
_ = changeStream.kill()
}
}.flatMap { resumeToken in
inventory.watch(options: ChangeStreamOptions(resumeAfter: resumeToken)).flatMap { newStream in
newStream.forEach { event in
// process event
print(event)
}
}
}
// End Changestream Example 3
}

do {
// Start Changestream Example 4
let pipeline: [Document] = [
["$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
changeStream.next()
}

// Option 2: register a callback to execute for each document
let result = inventory.watch(pipeline, withEventType: Document.self).flatMap { changeStream in
changeStream.forEach { event in
// process event
print(event)
}
}
// End Changestream Example 4
}
}
Loading