MongoSwift

The official MongoDB driver for Swift.

Index

Documentation

The latest documentation 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:

  1. Create an account and login: jira.mongodb.org
  2. Navigate to the SWIFT project: jira.mongodb.org/browse/SWIFT
  3. 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.

Installation

MongoSwift works with Swift 4.0+.

FIRST: Install the MongoDB C Driver

Because the driver wraps the MongoDB C driver, 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.11.0.

On a Mac, you can install both components at once using Homebrew: brew install mongo-c-driver.

Or on Linux, use apt-get to install libmongoc (which includes libbson as a dependency) and pkg-config (which enables Swift Package Manager to find the components):

sudo apt-get install pkg-config
sudo apt-get install libmongoc-1.0.0

Alternatively, see the installation guide from libmongoc’s documentation.

Next, see instructions for installation with either Swift Package Manager or CocoaPods in the following sections.

NEXT: Install the Driver Using Swift Package Manager

Please make sure you have followed the instructions in the previous section on installing the MongoDB C Driver before proceeding.

The Swift Package Manager is integrated with the Swift build system in Swift 3.0+. See the documentation for more information.

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: "0.0.2"),
    ],
    targets: [
        .target(name: "MyPackage", dependencies: ["MongoSwift"])
    ]
)

Then run swift build to download, compile, and link all your dependencies.

OR: Install the Driver Using CocoaPods

Please make sure you have followed the instructions in the previous section on installing the MongoDB C Driver before proceeding.

CocoaPods is a dependency manager for Swift and Objective-C. You can install it by running gem install cocoapods. See the CocoaPods documentation for more information.

If you don’t already have a Podfile for your project, run pod init in the main directory to automatically create one with smart defaults. Add MongoSwift as follows:

platform :osx, '10.10'
use_frameworks!

target 'MyApp' do
    pod 'MongoSwift', '~> 0.0.2'
end

Finally, run pod install to install your project’s dependencies.

Example Usage

Initialization

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().

Connect to MongoDB and Create a Collection

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().

Create and Insert a Document

let doc: Document = ["_id": 100, "a": 1, "b": 2, "c": 3]
let result = try collection.insertOne(doc)
print(result?.insertedId ?? "") // prints `100`

Find Documents

let query: Document = ["a": 1]
let documents = try collection.find(query)
for d in documents {
    print(d)
}

Work With and Modify Documents

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}`

Development Instructions

See our development guide for instructions for building and testing the driver.