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
18 changes: 9 additions & 9 deletions Examples/Docs/Sources/DocsExamples/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -17,14 +17,14 @@ private func causalConsistency() throws {
)
let items = client1.db("test", options: dbOptions).collection("items")
try items.updateOne(
filter: ["sku": "111", "end": BSONNull()],
update: ["$set": ["end": currentDate] as Document],
filter: ["sku": "111", "end": .null],
update: ["$set": ["end": .datetime(currentDate)]],
session: s1
)
try items.insertOne(["sku": "nuts-111", "name": "Pecans", "start": 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()
let client2 = try SyncMongoClient()

// Start Causal Consistency Example 2
try client2.withSession(options: ClientSessionOptions(causalConsistency: true)) { s2 in
Expand All @@ -34,7 +34,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)
}
}
Expand All @@ -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
Expand Down Expand Up @@ -81,8 +81,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)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -108,27 +108,27 @@ 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
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
}
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]`
```
Expand Down