Docs Menu
Docs Home
/
MongoDB Drivers

MongoDB Swift Driver

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

Welcome to the documentation site for the official MongoDB Swift driver. You can add the driver to your application to work with MongoDB in Swift. Download it with the Swift Package Manager or set up a runnable project with examples from our Usage Guide.

Important

The Swift driver is no longer under active development as of 2022.

Tip

If you are looking for information about using Swift with MongoDB Realm, see the MongoDB Realm iOS SDK documentation.

See Installation

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

import MongoSwiftSync
defer {
// free driver resources
cleanupMongoSwift()
}
// replace the following string with your connection uri
let uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
let client = try MongoClient(uri)
// print a list of database names
print (try client.listDatabaseNames())
// your application logic
import MongoSwift
import NIO
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
// replace the following string with your connection uri
let uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
let client = try MongoClient(
uri,
using: elg
)
defer {
// clean up driver resources
try? client.syncClose()
cleanupMongoSwift()
// shut down EventLoopGroup
try? elg.syncShutdownGracefully()
}
// print a list of database names
print(try client.listDatabaseNames().wait())
// your application logic

Note

For information about connecting to Atlas Serverless, see the Serverless Instance Limitations page for the minimum driver version you need.

If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:

  1. Download the Community or Enterprise version of MongoDB Server.

  2. Install and configure MongoDB Server.

  3. Start the server.

Important

Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.

After you successfully start your MongoDB server, specify your connection string in your driver connection code.

If your MongoDB Server is running locally, you can use the connection string "mongodb://localhost:<port>" where <port> is the port number you configured your server to listen for incoming connections.

If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings.

To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.

The compatibility table in this section specifies the recommended version or versions of the MongoDB Swift driver for use with a specific version of MongoDB.

The first column lists the driver version.

Important

MongoDB ensures compatibility between the MongoDB Server and the drivers for three years after the server version's end of life (EOL) date. To learn more about the MongoDB release and EOL dates, see MongoDB Software Lifecycle Schedules.

Icon
Explanation
All features are supported.
The Driver version will work with the MongoDB version, but not all new MongoDB features are supported.
No mark
The Driver version is not tested with the MongoDB version.
Swift Driver Version
MongoDB 7.0
MongoDB 6.0
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
1.3.0 [1]
1.2.0 [1]
1.1.0 [2]
1.0.0 [2]

Because the Swift driver is not under active development, it has not been tested with MongoDB Server versions 7.0 or later.

The Swift driver is not compatible with MongoDB server versions older than 3.6.

[1](1, 2) Versions 1.2 and 1.3 do not include support for Client-Side Field Level Encryption, GridFS, and authentication using AWS IAM roles.
[2](1, 2) Versions 1.0 and 1.1 do not include support for OCSP, Client-Side Field Level Encryption, GridFS, and authentication using AWS IAM roles.

The MongoDB Swift driver requires Swift 5.1 or later.

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.

Back

Ruby Drivers

Next

TypeScript