我從我的mongoDB數據庫中檢索文檔,但我需要以JSON格式打印出來。按照MongoKitten文檔的建議,我將所有相關信息存儲在一個數組中。我想打印出數組中每個元素的全部內容,這是記住MongoDB的一個文檔。我的代碼如下:以JSON格式打印出MongoDB文檔
import Foundation
import MongoKitten
let myDatabase = try MongoKitten.Database("mongodb://taylor:[email protected]:29374/taylorswiftengine")
let myCollection = myDatabase["my_collection"]
Request.addHandler(forMethod: "GET", withRoute: "/:resource/:id1")
{
(routeParams:RouteParams) in
let myTopics = try! myCollection.find("topic" == "\(routeParams["id1"]!)")
let allTopics = Array(myTopics)
}
我的數據是這樣的:'[{ 「_id」:「A3D813E8-B16E- 4D55-BF94-101B3D0BD6EF「,」message「:」我愛Charzard「,」email「:」[email protected]「,」topic「:」pokemon「},{」_id「:」D6926E07-7797- 41DC-849B-3854543931E0「,」消息「:」我愛蛋「,」email「:」[email protected]「,」主題「:」pokemon「},{」_id「:」0EF27D36-58B3- 4AB8-917C-F9955BA75120「,」留言「:」我愛皮卡丘「,」email「:」[email protected]「,」主題「:」pokemon「},{」_id「:」6DB65277-E806- 43C1-997D-69A21A0E3B4F「,」消息「:」我愛Gastly「,」email「:」[email protected]「,」主題「:」pokemo n「}]' –
然後它應該與jsonserialization – Kingalione
它不會,它會導致致命錯誤:( –