0

我似乎无法使用 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'"
4

1 回答 1

0

正确答案是

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

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

于 2017-08-29T10:36:42.987 回答