Skip to content
Native Swift library for BSON (
Swift Ruby
Branch: master/6.0
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources/BSON Fix timestamp representation as date Jul 21, 2019
Tests Fix timestamp representation as date Jul 21, 2019
.gitignore Specify only the minimum SwiftNIO version Feb 23, 2019
.jazzy.yaml Added docs Nov 2, 2016
BSON.podspec Merge commit '944e193672f40dd40a40dbaa7ddedd653b7f0608' into master/6.0 Apr 8, 2019
LICENSE MIT Apr 14, 2016
Package.pins replaced Btree Mar 20, 2017
Package.swift no message Mar 15, 2019 Update Jun 16, 2017
codecov.yml ignore Tests in code coverage Feb 6, 2017


Swift 3.1 License Build Status

BSON 5 is the fastest BSON library speeding past all libraries including C. It's compliant to the whole C BSON specification test suite, and even passes official libraries in compliance slightly.

It's not only fast, it's also got an extremely easy and intuitive API for extraction.

BSON is parsed and generated as specified for version 1.1 of the BSON specification.


The supported method for using this library is trough the Swift Package manager, like this:

import PackageDescription

let package = Package(
    name: "MyApp",
    dependencies: [.Package(url: "", majorVersion: 5)]

Create Documents naturally:

var userDocument: Document = [
	"username": "Joannis",
	"online": true,
	"age": 20,
	"pi_constant": 3.14,
	"profile": [
		"firstName": "Joannis",
		"lastName": "Orlandos"

let favouriteNumbers: Document = [1, 3, 7, 14, 21, 24, 34]

userDocument["favouriteNumbers"] = favouriteNumbers

Access values in an array like you would in Swift Arrays and values in an object like a Dictionary.

let favouriteNumber = favouriteNumbers[0]
let usernameValue = userDocument["username"]

Extract types with simplicity:

let username = String(userDocument["username"]) // "Joannis"
let isOnline = Bool(userDocument["online"]) // true
let age = Int(userDocument["age"]) // 20
let pi = Double(userDocument["pi_constant"]) // 3.14

Chain subscripts easily to find results without a hassle as shown underneath using this JSON structure (assuming this is represented in BSON):

  "users": [
  		"username": "Joannis",
  		"profile": {
  		  "firstName": "Joannis",
  		  "lastName": "Orlandos"
  		"username": "Obbut",
  		"profile": {
  		  "firstName": "Robbert",
  		  "lastName": "Brandsma"
let obbutLastName = String(object["users"][1]["profile"]["lastName"]) // "Brandsma"

Check the documentation for more information.


The performance in this BSON library is thanks to specialized algorithms per-operation with a centralized cache for indexed information. BSON is 100% lazy, so if you don't read data, it doesn't cost any performance.

Supported Types

All non-deprecated BSON 1.1 types are supported.

  • Double
  • String
  • Document
  • Array
  • ObjectId
  • Bool
  • DateTime
  • 32-bit integer
  • 64-bit integer
  • Null value
  • Binary
  • Regular Expression
  • Min Key
  • Max Key
  • Timestamp
  • Javascript Code
  • Javascript Code with Scope
  • Decimal128
You can’t perform that action at this time.