The official MongoDB driver for Swift.
The latest documentation is available here.
Think you've found a bug? Want to see a new feature in mongo-swift-driver
? Please open a case in our issue management tool, JIRA:
- Create an account and login: jira.mongodb.org
- Navigate to the SWIFT project: jira.mongodb.org/browse/SWIFT
- Click Create Issue - Please provide as much information as possible about the issue and how to reproduce it.
Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the Core Server (i.e. SERVER) project are public.
MongoSwift
works with Swift 4.0+.
Installation on macOS and Linux is supported via Swift Package Manager.
The driver wraps the MongoDB C driver, and using it requires having the C driver's two components, libbson
and libmongoc
, installed on your system. The minimum required version of the C Driver is 1.13.0.
On a Mac, you can install both components at once using Homebrew:
brew install mongo-c-driver
.
On Linux: please follow the instructions from libmongoc
's documentation. Note that the versions provided by your package manager may be too old, in which case you can follow the instructions for building and installing from source.
See example installation from source on Ubuntu in Docker.
Please follow the instructions in the previous section on installing the MongoDB C Driver before proceeding.
Add MongoSwift to your dependencies in Package.swift
:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "MyPackage",
dependencies: [
.package(url: "https://github.com/mongodb/mongo-swift-driver.git", from: "VERSION.STRING.HERE"),
],
targets: [
.target(name: "MyPackage", dependencies: ["MongoSwift"])
]
)
Then run swift build
to download, compile, and link all your dependencies.
Installation is supported via CocoaPods.
The pod includes as a dependency an embedded version of the MongoDB C Driver, meant for use on these OSes.
Note: the embedded driver currently does not support SSL. See #141 and CDRIVER-2850 for more information.
Add MongoSwift
to your Podfile as follows:
platform :ios, '11.0'
use_frameworks!
target 'MyApp' do
pod 'MongoSwift', '~> VERSION.STRING.HERE'
end
Then run pod install
to install your project's dependencies.
You must call MongoSwift.initialize()
once at the start of your application to
initialize libmongoc
. This initializes global state, such as process counters. Subsequent calls will have no effect.
You should call MongoSwift.cleanup()
exactly once at the end of your application to release all memory and other resources allocated by libmongoc
. MongoSwift.initialize()
will not reinitialize the driver after MongoSwift.cleanup()
.
import MongoSwift
// initialize global state
MongoSwift.initialize()
let client = try MongoClient(connectionString: "mongodb://localhost:27017")
let db = try client.db("myDB")
let collection = try db.createCollection("myCollection")
// free all resources
MongoSwift.cleanup()
Note: we have included the client connectionString
for clarity, but if connecting to the default "mongodb://localhost:27017"
it may be omitted: let client = try MongoClient()
.
let doc: Document = ["_id": 100, "a": 1, "b": 2, "c": 3]
let result = try collection.insertOne(doc)
print(result?.insertedId ?? "") // prints `100`
let query: Document = ["a": 1]
let documents = try collection.find(query)
for d in documents {
print(d)
}
var doc: Document = ["a": 1, "b": 2, "c": 3]
print(doc) // prints `{"a" : 1, "b" : 2, "c" : 3}`
print(doc["a"] ?? "") // prints `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 {
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 {
return 0
}
return value * 2
}
print(doubled) // prints `[2, 4, 6, 8]`
Note that Document
conforms to Collection
, so useful methods from
Sequence
and
Collection
are
all available. However, runtime guarantees are not yet met for many of these
methods.
The Examples/
directory contains sample projects that use the driver with Kitura and Vapor.
See our development guide for instructions for building and testing the driver.