簡體   English   中英

如何從MongoKitten查詢中提取文檔數組

[英]How to extract an array of Documents from a MongoKitten query

我似乎無法使用MongoKitten從父文檔中檢索文檔數組。

退回文件:

    {"updated":{"$date":"2016-01-16T17:58:45.171+11:00"},
 "name":"XXXX",
 "status":true,
 "_id":{"$oid":"57297a76b30bbf896e0a1c55"},

 "groups":[
    {"name":"Configuration Testing", 
     "allow_auto_approval":false, 
     "_id":{"$oid":"5699ea252529119457a40a67"}, 
     "expected_users":30, 
     "code":"632DZ0"
    },
    {"name":"Solution Demonstration", 
     "allow_auto_approval":false, 
     "_id":{"$oid":"5699ea252529119457a40a68"}, 
     "expected_users":50, 
     "code":"632GN1"}
 ]
 }

Swift代碼遍歷子文檔數組:

let result = database.collection.findOne()

let groups = result["groups"]

for group in groups {
   print("group: \(group["code")")
}

我也嘗試過

   for (key, val) in result["groups"].documentValue {
            print("Value is \(val)")
   }

但這顯示語法錯誤

"Value of type 'Primitive?' has no member 'documentValue'"

正確答案是

for (key, val) in Document(doc["groups"]) ?? [] {
    print("Value is \(val)")
}

感謝https://github.com/OpenKitten/MongoKitten/issues/27#issuecomment-325623157

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM