-
-
Notifications
You must be signed in to change notification settings - Fork 35
Intermediate Document usage
Joannis Orlandos edited this page Aug 18, 2016
·
3 revisions
So now we're creating a new Array Document containing the numbers 1-10 like this:
var document: Document = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
]
If we want to append a new value to this array we can do it like this:
document.append(11)
document.append(12)
document.append(13)
document.append(14)
document.append(15)
or
document += [11, 12, 13, 14, 15]
Or if we want to append values to the Document as a Dictionary:
document.append(42, forKey: "universe")
Let's loop over our key-value pairs:
for (key, value) in document {
print("key: \(key)")
print("value: \(value)")
}
In an Array we'll have the key counting up from 0 and up. And in a Dictionary Document we'll see the keys as normal String names like username
, _id
and age
.
Now say that we want to convert this Document to binary for storage or transfer over a socket we can take it's bytes:
let data: [UInt8] = document.bytes
let documentClone = Document(data: data)
Or if we want to convert this Document to MongoDB Extended JSON:
let jsonString = document.makeExtendedJSON()
If you receive a Document and instantiate it with Document(data: ...)
it's not validated yet. You can do this explicitly by calling document.validate()
.