{"id":988,"url":"https://github.com/Danappelxx/SwiftMongoDB","last_synced_at":"2025-07-30T20:30:43.137Z","repository":{"id":36647761,"uuid":"40954049","full_name":"Danappelxx/SwiftMongoDB","owner":"Danappelxx","description":"A MongoDB interface for Swift [Not under active development]","archived":true,"fork":false,"pushed_at":"2016-09-09T05:47:00.000Z","size":1063,"stargazers_count":266,"open_issues_count":10,"forks_count":25,"subscribers_count":10,"default_branch":"linux","last_synced_at":"2024-08-14T14:05:51.463Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Danappelxx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-18T06:09:10.000Z","updated_at":"2024-05-13T10:13:33.000Z","dependencies_parsed_at":"2022-11-29T13:19:52.251Z","dependency_job_id":null,"html_url":"https://github.com/Danappelxx/SwiftMongoDB","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Danappelxx%2FSwiftMongoDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Danappelxx%2FSwiftMongoDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Danappelxx%2FSwiftMongoDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Danappelxx%2FSwiftMongoDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Danappelxx","download_url":"https://codeload.github.com/Danappelxx/SwiftMongoDB/tar.gz/refs/heads/linux","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228179095,"owners_count":17881134,"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":[],"created_at":"2024-01-05T20:15:36.370Z","updated_at":"2024-12-04T19:32:33.924Z","avatar_url":"https://github.com/Danappelxx.png","language":"Swift","funding_links":[],"categories":["Database","Databases","Data and Storage"],"sub_categories":["Getting Started","Other free courses","Linter"],"readme":"# MongoDB \n\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![Join the chat at https://gitter.im/Danappelxx/SwiftMongoDB](https://img.shields.io/badge/gitter-join%20chat-blue.svg)](https://gitter.im/Danappelxx/SwiftMongoDB?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge) [![Travis CI](https://api.travis-ci.org/Danappelxx/SwiftMongoDB.svg)](https://travis-ci.org/Danappelxx/SwiftMongoDB) [![codecov.io](https://codecov.io/github/Danappelxx/SwiftMongoDB/coverage.svg?branch=master)](https://codecov.io/github/Danappelxx/SwiftMongoDB?branch=master) [![Platforms](https://img.shields.io/badge/Platforms-OS%20X%20--%20Linux-lightgray.svg?style=flat)](https://swift.org)\n\n\u003e#This library is no longer under active development. I highly recommend using the robust, pure-swift alternative [MongoKitten](https://github.com/OpenKitten/MongoKitten).\n\nA Swift MongoDB driver. Wraps around the [MongoDB C Driver 1.2.0](http://api.mongodb.org/c/1.2.0/) (supports MongoDB version 2.4 and later).\n\nSupports the 03-24 snapshot with 0.5, and 02-28 with 0.4. Earlier versions should not be used.\n\n# Setup\nThe simplest and linux-compatible way to use SwiftMongoDB is through the official Swift package manager. Simply add it to your `Package.swift` like so:\n\n```swift\nimport PackageDescription\nlet package = Package(\n  name: \"foo\",\n  dependencies: [\n    .Package(url: \"https://github.com/Danappelxx/SwiftMongoDB\", majorVersion: 0, minor: 5)\n  ]\n)\n```\nAs a second step you need to ensure that the mongo-c driver is installed on your system.\n## OSX (via brew)\n```\nbrew install mongo-c\n```\nIf you also need to install MongoDB run the following\n```\nbrew install mongodb\n```\n## The rest of the universe\n[How to install MongoDB C driver.](http://api.mongodb.org/c/current/)\n\n# Tutorial\nThis example assumes that you have setup the project and have MongoDB running.\n\n## Setup\nAt the top of any file where you use SwiftMongoDB, you need to import it:\n\n```swift\nimport MongoDB\n```\n\nFirst, you need to establish a connection to the MongoDB server.\n\n```swift\nlet client = try Client(host: \"localhost\", port: 27017)\n```\n\nYou can connect to a MongoDB instance (local or not) this way.\n\nThen you need to create a database.\n\n```swift\nlet testDatabase = Database(client: client, name: \"test\")\n```\n\nThen you need to create a collection. The collection doesn't have to exist (it will be created automatically if it isn't).\n\n```swift\nlet subjects = Collection(database: testDatabase, name: \"subjects\")\n```\n\n## Insertion and BSON\nAn important aspect of SwiftMongoDB is how it handles inserting documents. Most of the work will be done through a medium defined in [BinaryJSON](https://github.com/danappelxx/BinaryJSON) called `BSON.Document`. Creating it is very simple and type-safe, and the translation to MongoDB's lower-level type known as BSON is (fortunately) done behind the scenes for you.\n\nFor example, say you wanted to create a human named Dan. This is how it would look in JSON:\n\n```json\n{\n  \"name\" : \"Dan\",\n  \"age\" : 15,\n  \"lovesSwift\" : true,\n  \"location\" : {\n    \"state\" : \"California\"\n  },\n  \"favoriteNumbers\" : [1, 2, 3, 4, 5]\n}\n```\n\nIt looks pretty similar in Swift:\n\n```swift\nlet subject: BSON.Document = [\n    \"name\": \"Dan\",\n    \"age\": 15,\n    \"lovesSwift\" : true,\n    \"location\": [\n        \"state\": \"California\"\n    ],\n    \"favoriteNumbers\" : [1,2,3,4,5]\n]\n```\n\nYou can then insert the newly created document(s) into the collection.\n\n```swift\ntry subjects.insert(subject)\n```\n\nIf you want to create a `BSON.Document` from JSON, it's just as simple:\n\n```swift\nlet document = try BSON.fromJSONString(jsonString)\n```\n\nThat's it for inserting documents - pretty neat, right?\n\n## Querying\nNow, lets do some querying!\n\nHere's the most basic query you can possibly perform:\n\n```swift\nlet cursor = try subjects.find()\nlet testSubjects = try cursor.all()\n```\n\nThe first line, as you can see, returns a cursor. The `Cursor` type gives you more control over how you process the results of the query. For most use cases, the methods `Cursor.nextDocument` and `Cursor.all` will be enough. However, `Cursor` also conforms to `GeneratorType` and `SequenceType`, meaning that you can take advantage of a lot of neat Swift features. For example, you can iterate directly over it using a for loop:\n\n```swift\nlet results = try subjects.find()\nfor subject in results {\n    print(subject)\n}\n```\n\nNow, say we inserted a few more test subjects and wanted to query for all of them whose age is exactly 15. This is pretty simple:\n\n```swift\nlet result = try subjects.find(query: [\"age\" : 15])\n```\n\nThe query parameter operates just as it would in most other MongoDB implementations. For instance, in the MongoDB shell, this would be the equivalent of `db.subjects.find( {\"age\" : 15} )`.\n\n## Updating\nIf you wanted to change all test subjects who loved Swift to Chris Lattner, you could simply do:\n\n```swift\nlet newDocument: BSON.Document = [\n    \"name\": \"Chris Lattner\" // we can ignore the other keys for this example\n]\ntry subjects.update(query: [\"lovesSwift\": true], newValue: newDocument)\n```\n\n## Removing\nRemoving documents works the same way - if you want to remove all of the test subjects named Dan, you simply do:\n\n```swift\ntry subjects.remove(query: [\"name\" : \"Dan\"])\n```\n\nThose are the basics - it's really a very small simple library. You can take a look at the test suite and/or source which should show you some of the methods not shown here. Otherwise, feel free to jump right into the deep end and start using SwiftMongoDB!\n\n# Contributing\nAny and all help is very welcome! Feel free to fork and submit a pull request - I will almost certainly merge it.\n\nYou should start by taking a look at the current issues and see if there's anything you want to implement/fix there.\n\n# License\nMIT - more information is in the LICENSE file.\n\n# Changelog\n## 0.5\n### 0.5.0\nSwift 3 support\n\n## 0.4\n### 0.4.1\nMinor api fixes\n\n### 0.4.0\nA large refactor with a cleaner API and better internal code.\n\n## 0.3\n### 0.3.0\nSPM and Linux support\n\n## 0.2\n### 0.2.3\nUpdate dependencies, fix a few bugs.\n\n### 0.2.2\nUpdate dependencies.\n\n### 0.2.1\nSet up proper dependency management through Carthage, among other fixes.\n\n### 0.2.0\nMigrate to MongoDB C Driver 1.2.0 from 0.8, comes with a complete rewrite\n\n## 0.1\n### 0.1.1\nMigrate to Swift 2 error handling model\n\n### 0.1.0\nAdd documentation, clean up a lot of code, add examples for schemas using inheritance, protocols, and protocol extensions.\n\n## 0.0\n### 0.0.9\nAdd support for very simple mongoose-esque schemas by conforming to protocol MongoObject.\n\n### 0.0.8\nImplement (untested) login, fix major issue with querying where objects would either get ignored or query would loop indefinitely.\n\n### 0.0.7\nFix BSON encoder and decoder to support boolean values, bugfixes.\n\n### 0.0.6\nImplement BSON -\u003e Swift, bugfixes.\n\n### 0.0.5\nMake SwiftMongoDB multiplatform.\n\n### 0.0.4 - 0.0.2\nGetting Cocoapods to work, bugfixes.\n\n### 0.0.1\nCore operations implemented (insert, find, remove).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDanappelxx%2FSwiftMongoDB","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDanappelxx%2FSwiftMongoDB","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDanappelxx%2FSwiftMongoDB/lists"}