如何从MongoKitten查询中提取文档数组

时间:2017-08-29 04:54:59

标签: swift mongokitten

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

1 个答案:

答案 0 :(得分:0)

正确的答案是

swedishWords = open("svenskaOrd.txt","r")

for line in swedishWords:
    if(len(line.decode("iso8859_10")) >= 4):
        print(len(line.decode("iso8859_10")))
        print(line.decode("iso8859_10"))

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