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

查看:37
本文介绍了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)
  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天全站免登陆