I have issues trying to install MongoSwift (The official MongoDB driver for Swift applications on macOS and Linux)

I have issues trying to install MongoSwift (The official MongoDB driver for Swift applications on macOS and Linux).

I mention this people because I see that they are related with Swift and works in MongoDB so maybe they will see the post and can give me some advice:

@kmahar @Michael_Lynn

Xcode > File > Swift Packages > Add Package Dependency

Choose Package Repository


    Rules: Version: Up to next Major > 1.3.1

Then I have the choice to choose the dependencies that I want to install:

- MongoSwift (Asynchronous API)

- MongoSwiftSync (Synchronous API)

I tried in one project choose MongoSwift and in another project choose MongoSwiftSync but in both projects I have the same errors:

- import MongoSwift // No such module 'MongoSwift' 

The No such module is because the following error:

The package product 'SwiftBSON' requires minimum platform version 13.0 for the iOS platform, but this target supports 9.0

In mongo-swift-driver (https://github.com/mongodb/mongo-swift-driver)

- Package.swift

        dependencies: [
            .package(url: "https://github.com/mongodb/swift-bson", .upToNextMajor(from: "3.0.0"))

The latest version of swift-bson in https://github.com/mongodb/swift-bson is 3.1.0

I am using Xcode Version 12.4 (12D4e) in a MacOS Catalina Version 10.15.7 (19H1715)

I need some help to solve this issue.

Hi @Manuel_Martin, thank you for getting in touch.

This error occurs because:

  • swift-bson, a dependency of mongo-swift-driver, has a minimum supported version of iOS 13
  • mongo-swift-driver does not specify a minimum supported iOS version, because it is not intended to be used directly in iOS applications, so Xcode assumes the minimum version to be iOS 9

Sorry that the error here isn’t very clear; I’m currently investigating to see if there’s some way we can specify a custom error message to emit in this case.

If you’d like to use the Swift driver for building an iOS app, the architecture I would recommend is to build a backend server using a web framework like Vapor which handles interactions with the database, and having your iOS app communicate with that backend via HTTP. We have an example app doing exactly that available here.

To name a few reasons why it is not recommended to use a database driver directly from an iOS app:

  1. This would require shipping database access credentials with your app source code, which is both insecure and also makes it difficult to rotate them without forcing users to upgrade to a newer version of your app. If you handle this in a backend, you do not need to distribute the credentials, and you can easily change them without your users needing to update their app.
  2. This would require allowing arbitrary IP addresses to connect to your database (since users could be using your app from any IP), which makes your database more vulnerable to attackers. If you use a backend, you only need to allow the IP address where the backend is running to connect to your database.
  3. If any instance of an iOS app is allowed to connect directly to your database you have no way of throttling the number of connections made to your database, which can overwhelm it. However, if all database interaction is handled via a unified backend with a single MongoClient, you can limit the number of concurrent database connections via options such as maxPoolSize. Also, a backend allows you to more efficiently reuse database connections since they can be re-used for requests from any user, not just the user on a particular iOS device.

Another alternative to consider if you don’t want to write a backend would be using Realm, which allows you to both store data on-device and also sync it to a MongoDB cluster running on MongoDB Atlas via Realm Sync.


Thank you very much for your detailed answer. :heart:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.