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

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

问题描述

我需要的是什么:



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





<但是,当我显示文章列表时,我不需要内容字段(以节省带宽)。我已经读过了(也许我错了),不可能通过Firestore查询文档中的特定字段。



如果是普通的SQL从文章中获取特定的列(没有它的内容)它会是这样的:

  SELECT title,creation_date,.. 。
FROM table_name;

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






我目前的结构:



文章集合:

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

内容集合:

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

要实时获取文章列表:

  firebase.firestore()。collection('articles')
.where(`owner。$ {user.uid}`,'==',true)
.onSnapshot(querySnapshot => {
const articles = []
querySnaps hot.forEach((doc)=> {
articles.push({
id:doc.id,
... doc.data()
})
})
//用文章数组做一些事情
})

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

  const db = firebase.firestore()
const articleRef = db.collection('articles' ).doc(articleId)
const contentRef = db.collection('contents')。doc(articleId)//与文章相同的ID

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






我的问题




  • 你认为做两个查询更好吗(getArtic le和getContent)同时使用Promise.all()而不是像我那样嵌套查询?


  • 有没有更好的方法来获取文章及其内容与一个查询或更有效?一些提示或想法?




非常感谢您提前!

解决方案

两种方法都没有比其他方法更好。但是有一些关键的区别。


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


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


最后,差异可能很小。但由于它们对您的用例可能很重要,因此请测量结果,看看哪种方法最适合您。


What I need:

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

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.

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)


My current structure:

Articles collection:

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

Contents collection:

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

To get articles list in realtime:

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
      }
    })
  } 
})


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?

Thank you very much in advance!

解决方案

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

  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.

  2. 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天全站免登陆