Firestore为noSQL和Flutter复制SQL Join [英] Firestore replicating a SQL Join for noSQL and Flutter

查看:100
本文介绍了Firestore为noSQL和Flutter复制SQL Join的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到在复制与NoSql文档数据库(如FireStore)的联接方面存在很多问题,但是我无法找到将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)时,已为用户John Smith(uid:xyc4567)创建了连接"文档.

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"

在Flutter我的Flutter应用中,我有一个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 Join的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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