首页 > 解决方案 > 如何在 Swift 中自动生成的键上查询OrderedByValue?

问题描述

我需要queryOrderedByValue()在其密钥是自动生成的节点上。我的数据库的结构如下图所示(/users-pings/$uid/$uid/"auto-id")。

我可以对下面的查询进行哪些更改以实现此目的?

REF_USERS_PINGS.child(currentUid).child(forId).queryOrderedByValue().queryEqual(toValue: "true").observeSingleEvent(of: .value) { (snapshot) in


            if snapshot.exists(){

                handler(true)

            } else {

                handler(false)
            }

在此处输入图像描述

标签: swiftfirebasefirebase-realtime-database

解决方案


Firebase 数据库查询在您运行它们的位置下的每个子节点上运行,因此user-pings在您的情况下它们会自动跳过第三级。

您只需要指定要订购/过滤的属性,这似乎就在active这里。所以像:

REF_USERS_PINGS.child(currentUid).child(forId).queryOrdered(byChild: "active").queryEqual(toValue: "true").observeSingleEvent(of: .value) { (snapshot) in

推荐阅读