Firestore - 从文档中获取特定字段 [英] Firestore - get specific fields from document

查看:16
本文介绍了Firestore - 从文档中获取特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Firestore 中保存文章或笔记及其各自的字段:

I want to save articles or notes in Firestore with their respective fields:

但是当我显示文章列表时,我不需要内容"字段(以节省带宽).我已经读过(也许我错了),无法使用 Firestore 进行查询以仅从文档中获取特定字段.

But when I show the list of articles I don't need the "content" field (to save bandwidth). I've read that (maybe I'm wrong), it is not possible to make a query to get only specific fields from a document with Firestore.

如果从文章中获取特定列(没有其内容)是正常的 SQL 的话,它会是这样的:

If it were normal SQL to obtain specific columns from articles (without its content) It would be something like:

SELECT title, creation_date, ...
FROM table_name;

所以我选择将两个根级集合的内容分开(为了灵活性和可扩展性)

So I've opted to separate the content for two root-level collections (for flexibility and scalability)

文章集:

  - `articles` [collection]
    - `ARTICLE_ID` [document]
      - `creatorId` [field]
      - `title` [field]
      - `date` [field]
      - `owners` [obj field]
          - {user1_id}: true
          - {user2_id}: true
          ...

内容集合:

  - `contents` [collection]
    - `{ARTICLE_ID}` [document]
      - `content` [field]

实时获取文章列表:

firebase.firestore().collection('articles')
  .where(`owners.${user.uid}`, '==', true)
  .onSnapshot(querySnapshot => {
    const articles = []
    querySnapshot.forEach((doc) => {
      articles.push({
        id: doc.id,
        ...doc.data()
      })
    })
    // do something with articles array
  })

要在另一个视图中显示并获取整篇文章及其内容:

To show in another view and get the entire article with its content:

const db = firebase.firestore()
const articleRef = db.collection('articles').doc(articleId)
const contentRef = db.collection('contents').doc(articleId) // same Id as article

articleRef.get().then(articleDoc => {
  if (articleDoc.exists) {
    contentRef.get().then(contentDoc => {
      if (contentDoc.exists) {
        const article = {
          ...articleDoc.data(),
          ...contentDoc.data()
        }
        // full article obj
      }
    })
  } 
})

<小时>

我的问题

  • 您是否认为最好同时执行两个查询(getArticle 和 getContent)并使用 Promise.all() 等待而不是像我那样嵌套查询?


    My questions

    • Do you think it's better to do two queries (getArticle and getContent) at the same time and wait with Promise.all() instead of nesting the querys like I do?

      是否有更好的方法可以通过一个查询或更有效地获取文章及其内容?一些提示或想法?

      Is there a better way to get the article and its content with one query or more efficiently? Some tips or ideas?

      在此先非常感谢您!

      推荐答案

      两种方法都没有明显优于另一种方法.但有一些关键差异.

      Neither approach is pertinently better than the other. But there are a few key differences.

      1. 嵌套读取时,第二次读取仅在第一次读取完成后开始.当您使用 Promise.all() 时,两个读取同时开始,因此可以(部分)并行运行.

      1. When you nest the reads, the second read only starts after the first read has completed. When you use Promise.all() both reads start at the same time, so can (partially) run in parallel.

      另一方面:当您使用 Promise.all() 时,您的完成处理程序(您在 then() 中运行的代码)将不会执行直到两个文档都加载完毕.如果您嵌套调用,则可以在第一个文档加载后更新 UI.

      On the other hand: when you use Promise.all() your completion handler (the code you run in then()) won't execute until both documents have loaded. If you nest the calls, you can update the UI after just the first document has loaded.

      最终,差异可能很小.但由于它们可能对您的用例很重要,因此请衡量结果并查看最适合您的方法.

      In the end, the differences are likely to be small. But since they may be significant to your use-case, measure the results and see what works best for you.

      这篇关于Firestore - 从文档中获取特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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