Firestore 为 noSQL 和 Flutter 复制 SQL 连接 [英] Firestore replicating a SQL Join for noSQL and Flutter
问题描述
我意识到在使用 FireStore 等 NoSql 文档数据库复制连接方面存在很多问题,但是我无法找到将 Dart/Flutter 与 FireStore 结合使用的彻底解决方案.
I realise there is many questions in regards to replicating joins with NoSql document databases such as FireStore, however i'm unable to find a thorough solution utilising Dart/Flutter with FireStore.
我做了一些研究,我觉得在下面的例子中我会寻找一个多对多"的关系(如果这是错误的,请纠正我)因为将来可能需要查看所有配置文件作为所有连接.
I have done some research i feel that in the following example i would be looking for a 'many to many' relationship (please correct me if this is wrong) as there may be a future need to look at all profiles as well as all connections.
在 firebase 中,我有两个根级集合(配置文件和连接):
In firebase, i have two root level collections (profile & connection):
profile
> documentKey(Auto Generated)
> name = "John Smith"
> uid = "xyc4567"
> documentKey(Auto Generated)
> name = "Jane Doe"
> uid = "abc1234"
> documentKey(Auto Generated)
> name = "Kate Dee"
> uid = "efg8910"
connection
> documentKey(Auto Generated)
> type = "friend"
> profileuid = "abc1234"
> uid = "xyc4567"
> documentKey(Auto Generated)
> type = "family"
> profileuid = "abc1234"
> uid = "efg8910"
对于此示例,假设用户 John Smith (uid: xyc4567) 连接到 Jane Doe (uid: abc1234) 和 Kate Dee (uid: efg8910) 时创建了连接"文档.
For this example the 'connection' documents have been created hypothetically for the user John Smith (uid: xyc4567) when he connected to Jane Doe (uid: abc1234) and Kate Dee (uid: efg8910).
这是我希望复制的关系 SQL,以显示 John Smith 已连接的配置文件列表:
Here is the relational SQL i'm looking to replicate to show a list of profiles which John Smith has connected with:
Select * FROM profile, connection
WHERE profile.uid = connection.profileuid
AND profile.uid = "xyc4567"
在颤振我的颤振应用程序中,我有一个 fireStore 查询起点:
In flutter my flutter app i have a fireStore query starting point:
stream: Firestore.instance.collection('profile')
.where('uid', isEqualTo: "xyc4567").snapshots(),
显然它只从一个集合返回.我如何以多对多关系加入集合?
Obviously it only returns from one collection. How do i join the collections in a many to many relationship?
推荐答案
很遗憾,Cloud Firestore 和其他 NoSQL 数据库中都没有 JOIN
子句.在 Firestore 中,查询很浅.这意味着他们只能从运行查询的集合中获取项目.无法在单个查询中从两个顶级集合中获取文档.Firestore 不支持一次性跨不同集合进行查询.单个查询只能使用单个集合中文档的属性.
Unfortunately, there is no JOIN
clause in Cloud Firestore nor in others NoSQL databases. In Firestore queries are shallow. This means that they only get items from the collection that the query is run against. There is no way to get documents from two top-level collection in a single query. Firestore doesn't support queries across different collections in one go. A single query may only use properties of documents in a single collection.
所以我能想到的最简单的解决方案是查询数据库以从 profile
集合中获取用户的 uid
.获得该 ID 后,进行另一个数据库调用(在回调中),并使用以下查询从 connection
集合中获取您需要的相应数据:
So the most simple solution I can think of is to query the database to get the uid
of a user from the profile
collection. Once you have that id, make another database call (inside the callback), and get the corresponding data that you need from the connection
collection using the following query:
stream: Firestore.instance.collection('connection').where('uid', isEqualTo: "xyc4567").snapshots(),
另一种解决方案是在每个用户下创建一个名为 connection
的子集合,并在其下添加所有 connection
对象.这种做法称为 denormalization
,是 Firebase 的常见做法.如果您是 NoQSL 数据库的新手,我建议您观看此视频,Firebase 数据库的非规范化是正常的 以便更好地理解.它适用于 Firebase 实时数据库,但同样的规则适用于 Cloud Firestore.
Another solution would be to create a subcollection named connection
under each user and add all connection
objects beneath it. This practice is called denormalization
and is a common practice when it comes to Firebase. If you are new to NoQSL databases, I recommend you see this video, Denormalization is normal with the Firebase Database for a better understanding. It is for Firebase realtime database but same rules apply to Cloud Firestore.
另外,当您复制数据时,需要牢记一件事.与添加数据的方式相同,您需要对其进行维护.换句话说,如果你想更新/删除一个项目,你需要在它存在的每个地方都这样做.
Also, when you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.
这篇关于Firestore 为 noSQL 和 Flutter 复制 SQL 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!