Navigation

    MongoDB Swift Driver

    Introduction

    This is the official MongoDB Swift Driver.

    Installation

    See Installation

    Connect to MongoDB Atlas

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

    import MongoSwiftSync
    
    defer {
        // free driver resources
        cleanupMongoSwift()
    }
    
    let client = try MongoClient("mongodb+srv://<username>:<password>@<cluster-url>/test?retryWrites=true&w=majority")
    let db = client.db("myDB")
    
    // your application logic
    import MongoSwift
    import NIO
    
    let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
    let client = try MongoClient(
        "mongodb+srv://<username>:<password>@<cluster-url>/test?retryWrites=true&w=majority",
        using: elg
    )
    
    defer {
        // clean up driver resources
        try? client.syncClose()
        cleanupMongoSwift()
    
        // shut down EventLoopGroup
        try? elg.syncShutdownGracefully()
    }
    
    let db = client.db("myDB")
    
    // your application logic

    Compatibility

    MongoDB Compatibility

    Swift DriverMongoDB 4.2MongoDB 4.0MongoDB 3.6MongoDB 3.4MongoDB 3.2MongoDB 3.0MongoDB 2.6
    1.0.0

    Language Compatibility

    The MongoDB Swift driver requires Swift 5.1 or later.

    How to get help