Firestore - 可以通过数组不包含查询吗? [英] Firestore - Possible to query by array-not-contains?

查看:20
本文介绍了Firestore - 可以通过数组不包含查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道通过数组中不存在的值进行查询会非常困难,但有没有办法做到这一点而不完全这样做?

I am aware it would be very difficult to query by a value that does not exist in an array but is there a way to do this without doing exactly that?

这是我的场景 - 我有一个基于订阅的服务,人们可以选择加入并关注"特定的艺术家.在我的后端,这会创建一个订阅文档,其中包含他们的 followerId、他们想要关注的艺术家的 id(称为artistId)以及一个称为 push 的数组.艺术家可以添加新版本,然后在未来向每个关注者发送特定歌曲的通知.我想跟踪哪个随从被推送到哪个版本,这在前面提到的推送数组中完成.我需要一种方法来找到哪些关注者已经被推送到特定版本,所以...

Here is my scenario - I have a subscription based service where people can opt in and "follow" a specific artist. In my backend, this creates a subscription doc with their followerId, the id of the artist they want to follow (called artistId), and an array called pushed. The artist can add new releases, and then send each follower a notification of a specific song in the future. I would like to keep track of which follower has been pushed which release, and this done in the aforementioned pushed array. I need a way to find which followers have already been pushed a specific release and so...

我正在考虑合并两个查询,但我不确定是否可行.类似的东西:

I was thinking of combining two queries but I am not sure if it is possible. Something like:

  1. db.collection('subscriptions').where('artistId', '==',artistId)
  2. db.collection('subscriptions').where('artistId', '==',artistId).where('pushed', 'array-contains', releaseId)

然后取两个查询结果的交集并从第一个查询中减去,以获得尚未推送特定版本的关注者.

And then take the intersection of both query results and subtract from the 1st query to get the followers that have not been pushed a specific release.

这可能吗?或者有更好的方法吗?

Is this possible? Or is there a better way?

推荐答案

无法在 Firestore 中查询具有特定字段或值的文档.这不是非常困难",而是根本不可能.要详细了解原因,请参阅:

There is no way to query Firestore for documents that don't have a certain field or value. It's not "very difficult", but simply not possible. To learn more on why that is, see:

  • Firestore get documents where value not in array?
  • Firestore: how to perform a query with inequality / not equals
  • The Get to know Cloud Firestore episode on how Firestore queries work.

您的解决方法是可行的,技术上甚至不是很复杂.唯一要记住的是,您需要加载所有艺术家.因此,性能将与您拥有的艺术家数量成线性关系.目前这对您的应用来说可能没问题,但肯定需要对其进行一些测量.

Your workaround is possible, and technically not even very complex. The only thing to keep in mind is that you'll need to load all artists. So the performance will be linear to the number of artists you have. This may be fine for your app at the moment, but it's something to definitely do some measurements on.

通常,我的解决方法不是跟踪什么发布给用户,而是跟踪上次推送给用户的时间.假设一个版本有一个通知发送时间戳",每个用户都有一个最后收到的通知时间戳".通过比较两者,您可以查询尚未收到有关特定版本通知的用户.

Typically my workaround is to track not what releases were pushed to a user, but when the last push was sent to a user. Say that a release has a "notifications sent timestamp" and each user has a "last received notifications timestamp". By comparing the two you can query for users who haven't received a notification about a specific release yet.

具体的数据模型取决于您的具体用例,您可能需要跟踪每个用户的多个时间戳(例如,他们关注的每个艺术家).但我发现一般我可以基于这个模型提出一个合理的解决方案.

The exact data model for this will depend on your exact use-case, and you might need to track multiple timestamps for each user (e.g. for each artist they follow). But I find that in general I can come up with a reasonable solution based on this model.

这篇关于Firestore - 可以通过数组不包含查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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