Firebase实时数据库:4级嵌套上的orderByChild()。equalTo()

时间:2019-04-24 09:45:18

标签: javascript firebase firebase-realtime-database firebase-admin

我正在尝试运行Firebase实时数据库查询,以根据从根目录嵌套在第4级的值来过滤数据。下面是数据结构,我的查询是:

let ordersSnapshot = await admin.database().ref(`orders`).orderByChild(`info/infoStatus`)
            .equalTo("In Process")
            .once('value');

但是此查询未返回任何数据。我已启用索引编制,并且没有警告。我也将查询更改为.orderByChild('infoStatus'),但没有结果。

如果我将我的裁判设置为低于admin.database().ref('orders/userId1').orderByChild('info/infoStatus').equalTo("In Process")的一级,那么它将成功获取结果。但是在我的情况下,我没有可以在orderByChild中使用的用户ID。

这是实时数据库查询的限制吗,即我需要更新数据结构还是查询中出现任何错误?

订单节点:

{
    "userId1": {
        "orderId1": {
            "info": {
                "infoStatus": "In Process"
            }
        },
        "orderId2": {
            "info": {
                "infoStatus": "Complete"
            }
        }
    },
    "userId2": {
        "orderId1": {
            "info": {
                "infoStatus": "In Process"
            }
        }
    },
    "userId3": {
        "orderId1": {
            "info": {
                "infoStatus": "In Process"
            }
        }
    }


}

1 个答案:

答案 0 :(得分:1)

用于订购/过滤的属性必须位于每个子项下的固定路径上。这意味着在您当前的数据结构中,您不能仅针对特定用户过滤所有用户的订单。

典型的解决方案是将数据展平,将订单存储在平面列表中,并在需要时向每个用户添加订单ID查找列表。像这样:

users: {
    "userId1": {
        "orderId1": true,
        "orderId2": true
    },
    "userId2": {
        "orderId3": true
    }
    "userId3": {
        "orderId4": true
    }
}
orders: {
    "orderId1": {
        "info": {
            "infoStatus": "In Process"
        }
    },
    "orderId2": {
        "info": {
            "infoStatus": "Complete"
        }
    },
    "orderId3": {
        "info": {
            "infoStatus": "In Process"
        }
    },
    "orderId4": {
        "info": {
            "infoStatus": "In Process"
        }
    }
}

另请参阅:Firebase Query Double Nested

相关问题