【问题标题】:How to extract an array of Documents from a MongoKitten query如何从 MongoKitten 查询中提取文档数组
【发布时间】:2017-08-29 04:54:58
【问题描述】:

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

【问题讨论】:

标签: swift mongokitten


【解决方案1】:

正确答案是

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

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

【讨论】:

    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 2015-06-23
    • 2018-08-15
    相关资源
    最近更新 更多