gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:12:03 24 4
gpt4 key购买 nike

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

关于swift - 如何从 MongoKitten 查询中提取文档数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45930918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com