MongoSwift
The official MongoDB driver for Swift applications on macOS and Linux.
Documentation
The latest documentation for the driver is available here.
The latest documentation for the driver's BSON library is available here.
Bugs / Feature Requests
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.
Security Concerns
Please see SECURITY.md for details on our security process.
Installation
The driver supports use with Swift 5.1+. The minimum macOS version required to build the driver is 10.14. The driver is tested in continuous integration against macOS 10.14, Ubuntu 16.04, and Ubuntu 18.04.
Installation is supported via Swift Package Manager.
You can find details about all our versions in this repo's releases page.
Step 1: Install Required System Libraries (Linux Only)
The driver vendors and wraps the MongoDB C driver (libmongoc
), which depends on a number of external C libraries when built in Linux environments. As a result, these libraries must be installed on your system in order to build MongoSwift.
To install those libraries, please follow the instructions from libmongoc
's documentation.
Step 2: Install the driver
The driver contains two modules to support a variety of use cases: an asynchronous API in MongoSwift
, and a synchronous API in MongoSwiftSync
. The modules share a number of core types such as options struct
s.
The driver depends on our library swift-bson
, containing a BSON implementation. All BSON symbols are re-exported from the drivers' modules, so you do not need to explicitly import BSON
in your application.
To install the driver, add the package and relevant module as a dependency in your project's Package.swift
file:
Then run swift build
to download, compile, and link all your dependencies.
Example Usage
Note: You should call cleanupMongoSwift()
exactly once at the end of your application to release all memory and other resources allocated by libmongoc
.
Connect to MongoDB and Create a Collection
Async:
Sync:
Note: we have included the client connectionString
parameter for clarity, but if connecting to the default "mongodb://localhost:27017"
it may be omitted.
Create and Insert a Document
Async:
Sync:
Find Documents
Async:
Sync:
Work With and Modify Documents
Note that BSONDocument
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.
Usage With Kitura, Vapor, and Perfect
The Examples/
directory contains sample projects that use the driver with Kitura, Vapor, and Perfect.
Please note that the driver is built using SwiftNIO 2, and therefore is incompatible with frameworks built upon SwiftNIO 1. SwiftNIO 2 is used as of Vapor 4.0 and Kitura 2.5.
Development Instructions
See our development guide for instructions for building and testing the driver.