All Projects → mongodb → Mongo Swift Driver

mongodb / Mongo Swift Driver

Licence: apache-2.0
The official MongoDB driver for Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Mongo Swift Driver

Mongokitten
Native MongoDB driver for Swift, written in Swift
Stars: ✭ 605 (+150%)
Mutual labels:  mongodb-driver, mongodb, swift-package-manager
Lua Mongo
MongoDB Driver for Lua
Stars: ✭ 81 (-66.53%)
Mutual labels:  mongodb-driver, mongodb
Avocado
Strongly-typed MongoDB driver for Rust
Stars: ✭ 70 (-71.07%)
Mutual labels:  mongodb-driver, mongodb
Mongo.migration
On-the-fly migrations with MongoDB C# Driver
Stars: ✭ 99 (-59.09%)
Mutual labels:  mongodb-driver, mongodb
Mongoc.jl
MongoDB driver for the Julia Language
Stars: ✭ 46 (-80.99%)
Mutual labels:  mongodb-driver, mongodb
Firebaseswift
Firebase REST API wrapper for use in server-side Swift
Stars: ✭ 54 (-77.69%)
Mutual labels:  server-side-swift, swift-package-manager
Erlmongo
Erlang driver for MongoDB with gridfs that works with maps and proplists
Stars: ✭ 90 (-62.81%)
Mutual labels:  mongodb-driver, mongodb
Mongo Rust Driver
The official MongoDB Rust Driver
Stars: ✭ 633 (+161.57%)
Mutual labels:  mongodb-driver, mongodb
Zhttp
基于swoole的异步轻量级web框架,内部封装协程异步非阻塞全套mysql、redis、mongo、memcached连接池,可以轻松start、reload、stop,加入数据库的查询模块,框架已经封装好近乎同步写法,底层异步调用
Stars: ✭ 131 (-45.87%)
Mutual labels:  async, mongodb
Mongodb Plugin
MongoDB Plugin for Java
Stars: ✭ 236 (-2.48%)
Mutual labels:  mongodb-driver, mongodb
Mongojs
Node.js module that implements the offical mongo api
Stars: ✭ 1,782 (+636.36%)
Mutual labels:  mongodb-driver, mongodb
Phalcon Mongodb Odm
MongoDB ODM for Phalcon framework for new mongodb php extension with query builder and rich functionality
Stars: ✭ 42 (-82.64%)
Mutual labels:  mongodb-driver, mongodb
Mongo Cxx Driver
C++ Driver for MongoDB
Stars: ✭ 792 (+227.27%)
Mutual labels:  mongodb-driver, mongodb
Egg Mongo Native
MongoDB egg.js plugin using native driver.
Stars: ✭ 69 (-71.49%)
Mutual labels:  mongodb-driver, mongodb
Mongo Php Driver
MongoDB PHP driver
Stars: ✭ 737 (+204.55%)
Mutual labels:  mongodb-driver, mongodb
Jsoncore
A Swift JSON parser that doesn't need Objective-C bridging and doesn't depend on Foundation
Stars: ✭ 86 (-64.46%)
Mutual labels:  server-side-swift, swift-package-manager
Sockets
🔌 Non-blocking TCP socket layer, with event-driven server and client.
Stars: ✭ 559 (+130.99%)
Mutual labels:  async, server-side-swift
Mongo Perl Driver
Perl driver for the MongoDB
Stars: ✭ 203 (-16.12%)
Mutual labels:  mongodb-driver, mongodb
Mongo Php Library
MongoDB PHP library
Stars: ✭ 1,391 (+474.79%)
Mutual labels:  mongodb-driver, mongodb
Fooproxy
稳健高效的评分制-针对性- IP代理池 + API服务,可以自己插入采集器进行代理IP的爬取,针对你的爬虫的一个或多个目标网站分别生成有效的IP代理数据库,支持MongoDB 4.0 使用 Python3.7(Scored IP proxy pool ,customise proxy data crawler can be added anytime)
Stars: ✭ 195 (-19.42%)
Mutual labels:  async, mongodb

Code Coverage sswg:sandbox|94x20

MongoSwift

The official MongoDB driver for Swift applications on macOS and Linux.

Index

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:

  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

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.

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 structs. 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:

// swift-tools-version:5.1
import PackageDescription

let package = Package(
    name: "MyPackage",
    dependencies: [
        .package(url: "https://github.com/mongodb/mongo-swift-driver.git", from: "VERSION.STRING.HERE"),
    ],
    targets: [
        // Async module
        .target(name: "MyAsyncTarget", dependencies: ["MongoSwift"]),
        // Sync module
        .target(name: "MySyncTarget", dependencies: ["MongoSwiftSync"])
    ]
)

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:

import MongoSwift
import NIO

let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
let client = try MongoClient("mongodb://localhost:27017", using: elg)

defer {
    // clean up driver resources
    try? client.syncClose()
    cleanupMongoSwift()

    // shut down EventLoopGroup
    try? elg.syncShutdownGracefully()
}

let db = client.db("myDB")
let result = db.createCollection("myCollection").flatMap { collection in
    // use collection...
}

Sync:

import MongoSwiftSync

defer {
    // free driver resources
    cleanupMongoSwift()
}

let client = try MongoClient("mongodb://localhost:27017")

let db = client.db("myDB")
let collection = try db.createCollection("myCollection")

// use collection...

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:

let doc: BSONDocument = ["_id": 100, "a": 1, "b": 2, "c": 3]
collection.insertOne(doc).whenSuccess { result in
    print(result?.insertedID ?? "") // prints `.int64(100)`
}

Sync:

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

Find Documents

Async:

let query: BSONDocument = ["a": 1]
// The `sort` option specifies the order in which results are returned
// via the cursor. In this case, `["_id": -1]` indicates that the documents will
// be returned in descending order according to the `_id` field.
let options = FindOptions(sort: ["_id": -1])
let result = collection.find(query, options: options).flatMap { cursor in
    cursor.forEach { doc in
        print(doc)
    }
}

Sync:

let query: BSONDocument = ["a": 1]
// The `sort` option specifies the order in which results are returned
// via the cursor. In this case, `["_id": -1]` indicates that the documents will
// be returned in descending order according to the `_id` field.
let options = FindOptions(sort: ["_id": -1])
let documents = try collection.find(query, options: options)
for d in documents {
    print(try d.get())
}

Work With and Modify Documents

var doc: BSONDocument = ["a": 1, "b": 2, "c": 3]

print(doc) // prints `{"a" : 1, "b" : 2, "c" : 3}`
print(doc["a"] ?? "") // prints `.int64(1)`

// Set a new value
doc["d"] = 4
print(doc) // prints `{"a" : 1, "b" : 2, "c" : 3, "d" : 4}`

// Using functional methods like map, filter:
let evensDoc = doc.filter { elem in
    guard let value = elem.value.asInt() else {
        return false
    }
    return value % 2 == 0
}
print(evensDoc) // prints `{ "b" : 2, "d" : 4 }`

let doubled = doc.map { elem -> Int in
    guard case let value = .int64(value) else {
        return 0
    }

    return Int(value * 2)
}
print(doubled) // prints `[2, 4, 6, 8]`

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].