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

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

问题描述

在这个问题中 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

决定坚持使用RTDB,直到在Firestore上持久化真正起作用为止.

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

公认的答案可以解决此问题,即使它不能解决所有问题,也可能会帮助其他人.

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.

推荐答案

对于复制某些项目数据来说,这似乎是一个很好的例子.我假设某些项目属性(例如名称")是不可变的,因此可以存储在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-或查询替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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