Firebase Firestore - OR 查询替代方案 [英] Firebase Firestore - OR query alternative

查看:28
本文介绍了Firebase Firestore - OR 查询替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题 Firebase Firestore - OR 查询中,他们说没有 OR 查询.这意味着我无法轻松查询 ID 位于我之前获得的数组中的项目.

In this question Firebase Firestore - OR query they said there is no OR query. This means I can't easily query for items that have an ID that is in an array I got earlier.

// What I want
documentRef
    .whereField("itemId", isEqualTo: "id1")
    .orWhereField("itemId", isEqualTo: "id3")

// or
documentRef
    .whereField("itemId", in: idArray)

既然这不起作用,那么查询我的数据哪个选项更好?

Since this doesn't work, what option is better to query my data?

  1. 获取所有数据并在本地检查

我可以获取我的所有记录并只检查设备上的 ID,但问题是它最多可以有 1000 条记录,而用户可能只需要其中的三个.

I could get all my records and just check the IDs on the device but the problem is that it can be up to 1000 records and the user might only need three of them.

  1. 对所有项目进行单独查询

我可以为用户需要的每个项目查询whereField("itemId", isEqualTo: "id").如果用户只需要一些项目,这会很好,但用户可能需要所有项目,并且此解决方案将比第一个解决方案慢很多.(除非你可以批量读取)

I could just do a query whereField("itemId", isEqualTo: "id") for each item that the user needs. This would be good if the user only needs a few of the items but it is possible the user needs all the items and this solution would become a lot slower than the first one. (unless you could batch reads)

  1. 移动数据

我想我可以将购买的数据移动到 Item 集合:Items/itemid/PurchasedBy/uid 但我想检索用户购买的 Item 但我不知道如何通过嵌套集合中的字段查询 Item.我也不知道这是否是数据的正确位置.

I think I could move the purchased data to the Item collection: Items/itemid/PurchasedBy/uid but I want to retrieve the Item that a user has bought and I don't know how to query an Item by a field inside a nested collection. I also don't know if this would be the correct place for the data.

我的数据:

Users/uid/PurchasedItems/itemid

    payedInMoney: "$0.00"
    payedInCoins: "100"
    itemId: "itemid"
    timeStamp: timestamp

Items/itemid

    itemId: "itemid"
    // Other item info

<小时>

我选择了第一个选项,因为我在 RTDB 上就是这样做的,但是即使持久化并使用 addSnapshotListener Firestore 甚至没有接近 RTDB 实现的速度.我记录了时间,看起来 Firestore 的速度至少是 RTDB 的 4 倍(两者都启用了持久性).

I made the first option because that is how I did it on RTDB but even with persistence on and using addSnapshotListener Firestore doesn't even come close to the speed of the RTDB implementation. I clocked the times and it looks like Firestore is at least 4x as slow as RTDB (both with persistence enabled).

// iPad 5 (2017) - iOS 11
RTDB:  50-100ms
FST:  350-450ms

// iPad Air (2014) - iOS 10
RTDB: 100-150ms
FST:  700-850ms

// iPad Mini (2012) - iOS 9
RTDB: 150-200ms
FST:  2900-3200ms

编辑 2:

决定坚持使用 RTDB,直到 Firestore 上的持久性真正起作用.

Decided to stick with RTDB until persistence on Firestore really works.

编辑 3:

接受的答案解决了这个问题中的问题,即使它不能解决我的所有问题,也可能对其他人有所帮助.

The accepted answer fixes the problem in this question and even though it doesn't fix all mine, it might help someone else.

在仅使用 Firestore 的一个查询进行测试后,结果仍然比在 RTDB 中执行嵌套查询需要更长的时间,所以我现在会坚持我的决定.

After tests using only one query with Firestore the result still takes longer to come through than doing the nested query in RTDB so I will stick with my decision for now.

推荐答案

这似乎是复制某些项目数据的好方法.我假设像name"这样的一些项目属性是不可变的,因此可以存储在 PurchasedItems 子集合中,而不必担心未来的更新.

This seems like a good case for duplicating some of the item data. I assume that some item properties like "name" are immutable and can therefore be stored in the PurchasedItems subcollection without worrying about future updates.

因此,如果您将一些数据添加到 PurchasedItems:

So if you add some of the data to PurchasedItems:

Users/uid/PurchasedItems/itemid

    // Existing 
    payedInMoney: "$0.00"
    payedInCoins: "100"
    itemId: "itemid"
    timeStamp: timestamp

    // Add these
    name: "Item Name"
    url: "http://foo.bar/123"

然后,您将有足够的信息在单个查询中显示用户购买的商品列表,而无需按 ID 查找每件商品.

Then you will have enough information to display the list of a user's purchased items in a single query without needing to also look up each item by ID.

这篇关于Firebase Firestore - OR 查询替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆