From 4d9d71f12d33ed4051ac0d8cccfa8d2e69992397 Mon Sep 17 00:00:00 2001 From: Joannis Orlandos Date: Sun, 9 Apr 2017 01:58:21 +0200 Subject: [PATCH] BSON information --- README.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3aaad1e..9c8bb12 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,72 @@ import PackageDescription let package = Package( name: "MyApp", - dependencies: [.Package(url: "https://github.com/OpenKitten/BSON.git", majorVersion: 4)] + dependencies: [.Package(url: "https://github.com/OpenKitten/BSON.git", majorVersion: 5)] ) ``` +Create Documents naturally: + +```swift +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. + +```swift +let favouriteNumber = favouriteNumbers[0] +let usernameValue = userDocument["username"] +``` + +Extract types with simplicity: + +```swift +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): + +```json +{ + "users": [ + { + "username": "Joannis", + "profile": { + "firstName": "Joannis", + "lastName": "Orlandos" + } + }, + { + "username": "Obbut", + "profile": { + "firstName": "Robbert", + "lastName": "Brandsma" + } + } + ] +} +``` + +```swift +let obbutLastName = String(object["users"][1]["profile"]["lastName"]) // "Brandsma" +``` + Check the [documentation](http://docs.openkitten.org/bson/) for more information. ### Supported Types