{"id":13907636,"url":"https://github.com/mongodb/mongo-swift-driver","last_synced_at":"2025-11-11T20:58:10.770Z","repository":{"id":37514555,"uuid":"118030974","full_name":"mongodb/mongo-swift-driver","owner":"mongodb","description":"The official MongoDB driver for Swift","archived":false,"fork":false,"pushed_at":"2023-08-28T23:54:19.000Z","size":39938,"stargazers_count":347,"open_issues_count":1,"forks_count":64,"subscribers_count":34,"default_branch":"main","last_synced_at":"2025-04-01T00:14:43.714Z","etag":null,"topics":["async","mongodb","mongodb-driver","server-side-swift","swift","swift-5","swift-package-manager"],"latest_commit_sha":null,"homepage":"https://mongodb.github.io/mongo-swift-driver/","language":"Swift","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mongodb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-01-18T19:55:11.000Z","updated_at":"2025-03-20T13:58:55.000Z","dependencies_parsed_at":"2024-04-09T01:59:20.028Z","dependency_job_id":null,"html_url":"https://github.com/mongodb/mongo-swift-driver","commit_stats":{"total_commits":821,"total_committers":29,"mean_commits":"28.310344827586206","dds":0.7174177831912303,"last_synced_commit":"aa55e1faf8ecb06abbc2182d67138eab9d2620fe"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongodb%2Fmongo-swift-driver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongodb%2Fmongo-swift-driver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongodb%2Fmongo-swift-driver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mongodb%2Fmongo-swift-driver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mongodb","download_url":"https://codeload.github.com/mongodb/mongo-swift-driver/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247181256,"owners_count":20897368,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","mongodb","mongodb-driver","server-side-swift","swift","swift-5","swift-package-manager"],"created_at":"2024-08-06T23:02:03.427Z","updated_at":"2025-11-11T20:58:10.718Z","avatar_url":"https://github.com/mongodb.png","language":"Swift","funding_links":[],"categories":["HarmonyOS","Libraries"],"sub_categories":["Windows Manager","Swift"],"readme":"# Development Pause\nWe are announcing our decision to stop development of the MongoDB server-side Swift driver. No further development, bug fixes, enhancements, documentation changes or maintenance will be provided by this project and pull requests will no longer be accepted.\n\nThere are still ways to use MongoDB with Swift:\n\n- Use the MongoDB driver with server-side Swift applications as is\n- Use the [MongoDB C Driver](https://www.mongodb.com/docs/drivers/c/) directly in your server-side Swift projects\n- Usage of another community Swift driver, mongokitten\n\nCommunity members and developers are welcome to fork our existing driver and add features as you see fit - the Swift driver is under the Apache 2.0 license and source code is available on GitHub. For those developing client/mobile applications, MongoDB offers the [Realm Swift SDK](https://www.mongodb.com/docs/realm/sdk/swift/) with real time sync to MongoDB Atlas.\n\nWe would like to take this opportunity to express our heartfelt appreciation for the enthusiastic support that the Swift community has shown for MongoDB. Your loyalty and feedback have been invaluable to us throughout our journey, and we hope to resume development on the server-side Swift driver in the future.\n\n\n[![sswg:incubating|104x20](https://img.shields.io/badge/sswg-incubating-blue.svg)](https://github.com/swift-server/sswg/blob/main/process/incubation.md#incubating-level)\n\n# MongoSwift\nThe official [MongoDB](https://www.mongodb.com/) driver for Swift applications on macOS and Linux.\n\n### Index\n- [Documentation](#documentation)\n- [Bugs/Feature Requests](#bugs--feature-requests)\n- [Installation](#installation)\n    - [Step 1: Install Required System Libraries (Linux Only)](#step-1-install-required-system-libraries-linux-only)\n    - [Step 2: Install MongoSwift](#step-2-install-the-driver)\n- [Example Usage](#example-usage)\n    - [Connect to MongoDB and Create a Collection](#connect-to-mongodb-and-create-a-collection)\n    - [Create and Insert a Document](#create-and-insert-a-document)\n    - [Find Documents](#find-documents)\n    - [Work With and Modify Documents](#work-with-and-modify-documents)\n    - [Usage With Kitura, Vapor, and Perfect](#usage-with-kitura-vapor-and-perfect)\n- [Development Instructions](#development-instructions)\n\n## Documentation\nThe latest documentation for the driver is available [here](https://mongodb.github.io/mongo-swift-driver/).\nThe latest documentation for the driver's BSON library is available [here](https://mongodb.github.io/swift-bson/).\n\n## Bugs / Feature Requests\n\nThink 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:\n\n1. Create an account and login: [jira.mongodb.org](https://jira.mongodb.org)\n2. Navigate to the SWIFT project: [jira.mongodb.org/browse/SWIFT](https://jira.mongodb.org/browse/SWIFT)\n3. Click **Create Issue** - Please provide as much information as possible about the issue and how to reproduce it.\n\nBug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the\nCore Server (i.e. SERVER) project are **public**.\n\n## Security Concerns\nPlease see [SECURITY.md](SECURITY.md) for details on our security process.\n\n## Installation\nThe 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 11 and 12, and Ubuntu 18.04 and 20.04.\n\nInstallation is supported via [Swift Package Manager](https://swift.org/package-manager/).\n\nYou can find details about all our versions in this repo's [releases page](https://github.com/mongodb/mongo-swift-driver/releases).\n\n### Step 1: Install Required System Libraries (Linux Only)\nThe 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.\n\nTo install those libraries, please follow the [instructions](http://mongoc.org/libmongoc/current/installing.html#prerequisites-for-libmongoc) from `libmongoc`'s documentation.\n\n### Step 2: Install the driver\nThe 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.\nThe 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.\n\nTo install the driver, add the package and relevant module as a dependency in your project's `Package.swift` file:\n\n```swift\n// swift-tools-version:5.1\nimport PackageDescription\n\nlet package = Package(\n    name: \"MyPackage\",\n    platforms: [\n        .macOS(.v10_14) // minimum macOS version driver supports\n    ],\n    dependencies: [\n        .package(url: \"https://github.com/mongodb/mongo-swift-driver\", .upToNextMajor(from: \"1.3.1\"))\n    ],\n    targets: [\n        // Async module\n        .target(name: \"MyAsyncTarget\", dependencies: [\"MongoSwift\"]),\n        // Sync module\n        .target(name: \"MySyncTarget\", dependencies: [\"MongoSwiftSync\"])\n    ]\n)\n```\n\nThen run `swift build` to download, compile, and link all your dependencies.\n\n## Example Usage\n\nNote: You should call `cleanupMongoSwift()` exactly once at the end of your application to release all memory and other resources allocated by `libmongoc`.\n\n### Connect to MongoDB and Create a Collection\n\n**Async/Await (recommended)**:\n```swift\nimport MongoSwift\nimport NIOPosix\n\nlet elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)\nlet client = try MongoClient(\"mongodb://localhost:27017\", using: elg)\n\ndefer {\n    // clean up driver resources\n    try? client.syncClose()\n    cleanupMongoSwift()\n\n    // shut down EventLoopGroup\n    try? elg.syncShutdownGracefully()\n}\n\nlet db = client.db(\"myDB\")\nlet collection = try await db.createCollection(\"myCollection\")\n// use collection...\n```\n\n**Async (`EventLoopFuture`s)**:\n```swift\nimport MongoSwift\nimport NIOPosix\n\nlet elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)\nlet client = try MongoClient(\"mongodb://localhost:27017\", using: elg)\n\ndefer {\n    // clean up driver resources\n    try? client.syncClose()\n    cleanupMongoSwift()\n\n    // shut down EventLoopGroup\n    try? elg.syncShutdownGracefully()\n}\n\nlet db = client.db(\"myDB\")\n\nlet result = db.createCollection(\"myCollection\").flatMap { collection in\n    // use collection...\n}\n```\n\n**Sync**:\n```swift\nimport MongoSwiftSync\n\ndefer {\n    // free driver resources\n    cleanupMongoSwift()\n}\n\nlet client = try MongoClient(\"mongodb://localhost:27017\")\n\nlet db = client.db(\"myDB\")\nlet collection = try db.createCollection(\"myCollection\")\n\n// use collection...\n```\n\nNote: we have included the client `connectionString` parameter for clarity, but if connecting to the default `\"mongodb://localhost:27017\"`it may be omitted.\n\n### Create and Insert a Document\n**Async/Await (recommended)**:\n```swift\nlet doc: BSONDocument = [\"_id\": 100, \"a\": 1, \"b\": 2, \"c\": 3]\nlet result = try await collection.insertOne(doc)\nprint(result?.insertedID ?? \"\") // prints `.int64(100)`\n```\n\n**Async (`EventLoopFuture`s)**:\n```swift\nlet doc: BSONDocument = [\"_id\": 100, \"a\": 1, \"b\": 2, \"c\": 3]\ncollection.insertOne(doc).whenSuccess { result in\n    print(result?.insertedID ?? \"\") // prints `.int64(100)`\n}\n```\n\n**Sync**:\n```swift\nlet doc: BSONDocument = [\"_id\": 100, \"a\": 1, \"b\": 2, \"c\": 3]\nlet result = try collection.insertOne(doc)\nprint(result?.insertedID ?? \"\") // prints `.int64(100)`\n```\n\n### Find Documents\n**Async/Await (recommended)**:\n```swift\nlet query: BSONDocument = [\"a\": 1]\n// The `sort` option specifies the order in which results are returned\n// via the cursor. In this case, `[\"_id\": -1]` indicates that the documents will\n// be returned in descending order according to the `_id` field.\nlet options = FindOptions(sort: [\"_id\": -1])\nfor try await doc in try await collection.find(query, options: options) {\n    print(doc)\n}\n```\n\n**Async (`EventLoopFuture`s)**:\n```swift\nlet query: BSONDocument = [\"a\": 1]\n// The `sort` option specifies the order in which results are returned\n// via the cursor. In this case, `[\"_id\": -1]` indicates that the documents will\n// be returned in descending order according to the `_id` field.\nlet options = FindOptions(sort: [\"_id\": -1])\nlet result = collection.find(query, options: options).flatMap { cursor in\n    cursor.forEach { doc in\n        print(doc)\n    }\n}\n```\n\n**Sync**:\n```swift\nlet query: BSONDocument = [\"a\": 1]\n// The `sort` option specifies the order in which results are returned\n// via the cursor. In this case, `[\"_id\": -1]` indicates that the documents will\n// be returned in descending order according to the `_id` field.\nlet options = FindOptions(sort: [\"_id\": -1])\nlet documents = try collection.find(query, options: options)\nfor d in documents {\n    print(try d.get())\n}\n```\n\n### Work With and Modify Documents\n```swift\nvar doc: BSONDocument = [\"a\": 1, \"b\": 2, \"c\": 3]\n\nprint(doc) // prints `{\"a\" : 1, \"b\" : 2, \"c\" : 3}`\nprint(doc[\"a\"] ?? \"\") // prints `.int64(1)`\n\n// Set a new value\ndoc[\"d\"] = 4\nprint(doc) // prints `{\"a\" : 1, \"b\" : 2, \"c\" : 3, \"d\" : 4}`\n\n// Using functional methods like map, filter:\nlet evensDoc = doc.filter { elem in\n    guard let value = elem.value.asInt() else {\n        return false\n    }\n    return value % 2 == 0\n}\nprint(evensDoc) // prints `{ \"b\" : 2, \"d\" : 4 }`\n\nlet doubled = doc.map { elem -\u003e Int in\n    guard case let value = .int64(value) else {\n        return 0\n    }\n\n    return Int(value * 2)\n}\nprint(doubled) // prints `[2, 4, 6, 8]`\n```\n\nNote that `BSONDocument` conforms to `Collection`, so useful methods from\n[`Sequence`](https://developer.apple.com/documentation/swift/sequence) and\n[`Collection`](https://developer.apple.com/documentation/swift/collection) are\nall available. However, runtime guarantees are not yet met for many of these\nmethods.\n\n### Usage With Kitura, Vapor, and Perfect\nThe `Examples/` directory contains sample web application projects that use the driver with [Kitura](https://github.com/mongodb/mongo-swift-driver/tree/main/Examples/KituraExample), [Vapor](https://github.com/mongodb/mongo-swift-driver/tree/main/Examples/VaporExample), and [Perfect](https://github.com/mongodb/mongo-swift-driver/tree/main/Examples/PerfectExample).\nWe also have an example [full-stack Swift app](https://github.com/mongodb/mongo-swift-driver/tree/main/Examples/FullStackSwiftExample) with an iOS frontend and a backend built with Vapor.\n\nPlease 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.\n\n## Development Instructions\n\nSee our [development guide](https://github.com/mongodb/mongo-swift-driver/blob/main/Guides/Development.md) for instructions for building and testing the driver.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongodb%2Fmongo-swift-driver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmongodb%2Fmongo-swift-driver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmongodb%2Fmongo-swift-driver/lists"}