[英]What is better solution for online/offline user feature firestore or realtime?
I got a database of users.我有一个用户数据库。 Need to implement list which shows only online users.
需要实现只显示在线用户的列表。 Unfortunately realtime does not filter by key.
不幸的是,实时不能按键过滤。 So it is possible only in Firestore.
所以只有在 Firestore 中才有可能。 But Firestore charge for each write/read/update operation so it might be very expensive to use that feature since users are going offline/online all the time and there will be a lot of write/read/update operations.
但是 Firestore 对每个写入/读取/更新操作收费,因此使用该功能可能非常昂贵,因为用户一直处于离线/在线状态,并且会有很多写入/读取/更新操作。 As a solution, I did separate tables in realtime which saves only online users and here I don't need to pay for write/read/update.
作为一个解决方案,我实时做了单独的表,只保存在线用户,在这里我不需要为写/读/更新付费。 But at the same time, it is duplicating the database which is also not good practice.
但同时,它也在复制数据库,这也不是一个好习惯。
Could you please share your thought on which solution is better from "price" and "clean" point of view.您能否从“价格”和“清洁”的角度分享您对哪种解决方案更好的想法。 To pay for Firestore and use one db, or use two db but not to pay for each operation?
为 Firestore 付费并使用一个 db,还是使用两个 db 但不为每个操作付费?
You can perform filtering in FB RTDB, it is not as sophisticated or flexible as Firestore (you can only filter on one key or value at once) but if it is as trivial as your example then it is certainly possible (you need to use indexing to improve performance and limit data download when in production).您可以在 FB RTDB 中执行过滤,它不像 Firestore 那样复杂或灵活(您一次只能过滤一个键或一个值)但如果它像您的示例一样微不足道,那么它肯定是可能的(您需要使用索引以提高性能并限制生产时的数据下载)。
On a db structure like this (ignore the nodes below 'root' that are not 'users', I use this 'root' node for multiple code tests during my development):在这样的数据库结构上(忽略 'root' 下面不是 'users' 的节点,我在开发过程中使用这个 'root' 节点进行多个代码测试):
and using this code (in Flutter/Dart but the same parameters are available in other languages):并使用此代码(在 Flutter/Dart 中,但相同的参数在其他语言中可用):
_referenceRoot
.child("root")
.child("users")
.orderByChild("gender")
.equalTo("male")
.once()
.then(
(DataSnapshot snapshot) {
print("snapshot.value: ${snapshot.value}");
if (snapshot.value != null) {
print("snapshot.key: ${snapshot.key}");
}
},
);
I get this output ie.only the male users.我得到这个输出 ie.only 男性用户。 Note that in the snapshot the selected users are unordered:
请注意,在快照中,所选用户是无序的:
I/flutter ( 4307): snapshot.value: {a2: {hair: brown, gender: male, age: 20}, a3: {hair: brown, gender: male, age: 20}, a4: {hair: brown, gender: male, age: 20}, a6: {hair: brown, gender: male, age: 20}, a8: {hair: brown, gender: male, age: 20}, a9: {hair: brown, gender: male, age: 20}, a0: {hair: brown, gender: male, age: 20}} I/flutter ( 4307): snapshot.key: users
I/flutter(4307):snapshot.value:{a2:{hair:brown,gender:male,age:20},a3:{hair:brown,gender:male,age:20},a4:{hair:brown ,性别:男,年龄:20},a6:{头发:棕色,性别:男,年龄:20},a8:{头发:棕色,性别:男,年龄:20},a9:{头发:棕色,性别:男性,年龄:20},a0:{头发:棕色,性别:男性,年龄:20}} I/flutter(4307):snapshot.key:用户
See this for more details of what is possible: https://firebase.google.com/docs/database/rest/retrieve-data有关可能的更多详细信息,请参阅此内容: https : //firebase.google.com/docs/database/rest/retrieve-data
So, to answer your specific question, if the filtering you need to do is trivial, I would use only FB RTDB.因此,为了回答您的具体问题,如果您需要做的过滤很简单,我只会使用 FB RTDB。 If more complex, then will have to be Firestore.
如果更复杂,则必须是Firestore。 You would need to do the math on estimated data storage/download volumes vs. db calls and storage for a view on minimising costs.
您需要对估计的数据存储/下载量与 db 调用和存储进行数学计算,以最大限度地降低成本。
What is better solution for online/offline user feature Firestore or realtime?
在线/离线用户功能 Firestore 或实时有什么更好的解决方案?
It depends on what your requirements are, but you should not consider using one or the other.这取决于你的需求是什么,但你不应该考虑使用一个或另一个。 There is nothing wrong with using both.
两者都使用没有任何问题。
Unfortunately realtime does not filter by key.
不幸的是,实时不能按键过滤。
If you check the docs, there is a section named sorting and filtering data , where you'll find a method named orderByKey()
:如果您查看文档,会发现有一个名为排序和过滤数据的部分,您会在其中找到一个名为
orderByKey()
的方法:
Order results by child keys.
按子键排序结果。
So actually it is possible to filter the results of a query and order them in the same time.所以实际上可以过滤查询的结果并同时对它们进行排序。
As a solution I did the separate tables in realtime which saves only online users and here I don't need to pay for write/read/update.
作为一种解决方案,我实时制作了单独的表,只保存在线用户,在这里我不需要为写/读/更新付费。
That's a good solution you can go ahead with.这是一个很好的解决方案,您可以继续使用。 Actually, both Cloud Firestore and Firebase Realtime Database work very well together.
实际上,Cloud Firestore 和 Firebase 实时数据库可以很好地协同工作。
But at the same time, it is duplicating the database which is also not good practice.
但同时,它也在复制数据库,这也不是一个好习惯。
Oh, it is.哦,是的。 This practice is called denormalization and is a common practice when it comes to Firebase.
这种做法称为非规范化,是 Firebase 的常见做法。 For a better understanding, I recommend you see this video, Denormalization is normal with the Firebase Database and check my answer from the following post:
为了更好地理解,我建议您观看此视频, Firebase 数据库的非规范化是正常的,并从以下帖子中查看我的答案:
So you can denormalize the data to obtain queries that are actually not possible or to save some write operation when it comes to Cloud Firestore.因此,您可以对数据进行非规范化以获取实际上不可能的查询,或者在涉及 Cloud Firestore 时保存一些写入操作。
Could you please share your thought on which solution is better from "price" and "clean" point of view.
您能否从“价格”和“清洁”的角度分享您对哪种解决方案更好的想法。 To pay for Firestore and use one db, or use two db but not to pay for each operation?
为 Firestore 付费并使用一个 db,还是使用两个 db 但不为每个操作付费?
Use them both.使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.