简体   繁体   English

firestore:如何将 sql 加入来自 c# 的类似声明

[英]firestore: how do to a sql join like statement from c#

I have read about this a lot, but never in C#:我已经读过很多关于这方面的内容,但从未在 C# 中阅读过:

I am trying to access a user and his profile picture:我正在尝试访问用户及其个人资料图片:

tblUsers -> 1 (id) -> name: test, age: 12 tblUsers -> 1 (id) -> 名称:测试,年龄:12

tblUserPic -> 1 (id) ->picture: long pic, belongsTouserID: 1 tblUserPic -> 1 (id) ->picture: long pic, belongsTouserID: 1

So every user has a profile picture and I wanna "join" those tables together and get the user and his profile picture.所以每个用户都有一张个人资料图片,我想将这些表格“加入”在一起并获取用户和他的个人资料图片。

I was able to get either:我能够得到:

public static async Task<Dictionary<string, object>> GetElementById(FirestoreDb db, Documents collection, string documentId)
{
    DocumentReference docRef = db.Collection(collection.ToString()).Document(documentId);
    DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
    if (snapshot.Exists)
    {
        return snapshot.ToDictionary();
    }
    else
    {
        return null; ; 
    }
}

But never both.但绝不是两者兼而有之。

How can I "join"?我怎样才能“加入”? Or what is the fastest way of getting my data?或者获取我的数据的最快方法是什么?

Queries in Firestore are shallow, meaning that it can only return documents from the collection that the query is run against. Firestore 中的查询很浅,这意味着它只能从运行查询的集合中返回文档。 If you need to get documents from two collections, you'll have to perform two queries and combine the result on the client.如果您需要从两个 collections 获取文档,则必须执行两个查询并在客户端上合并结果。 There is no way you can perform an SQL JOIN.您无法执行 SQL JOIN。

But in your particular case, I would store the profile picture inside the User documents.但在您的特定情况下,我会将个人资料图片存储在用户文档中。 In this way, you only need to perform a single query.这样,您只需要执行一次查询。

What you just did, is called denormalization and it's common practice when it comes to NoSQL databases.您刚刚所做的称为反规范化,这是 NoSQL 数据库的常见做法。 But in this case, there is no need for that.但在这种情况下,没有必要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM