简体   繁体   English

如何从文档中获取特定字段值

[英]How to get specific field value from document

I'm trying to get values from documents in collection using Firestore.我正在尝试使用 Firestore 从集合中的文档中获取值。

This is my code and console log:这是我的代码和控制台日志:

 useEffect(() => {
    let totalarray = [];
   firebase.firestore().collection("users").doc(uid).collection("services").get().then((snapshot) => {
     snapshot.forEach((doc) => {
       const obj = doc.data();
       //obj.forEach((data) => totalarray.push((data)) )
      console.log("------->",obj);
     })
   })
  }, [])

This is my Console Log:这是我的控制台日志:

控制台日志

My Goal is to take the value of each "serviceCost" field for each doc, sum it up and then put it in a global variable for my use.我的目标是获取每个文档的每个“serviceCost”字段的值,将其相加,然后将其放入一个全局变量中供我使用。

If you mean that you would like to get just some parts of Firestore documents then you cannot.如果您的意思是您只想获取 Firestore 文档的某些部分,那么您不能。 you have to read the whole document then do your calculations client side.您必须阅读整个文档,然后在客户端进行计算。 other method, you may consider creating another collection like "totals".其他方法,您可以考虑创建另一个集合,如“总计”。 this collection will be populated by Firebase functions that invokes upon document creation and calculate the new cost then added it to the collection "totals"此集合将由 Firebase 函数填充,这些函数在创建文档时调用并计算新成本,然后将其添加到集合“总计”中

The following should do the trick:以下应该可以解决问题:

firebase.firestore().collection("users").doc(uid).collection("services").get().then((snapshot) => {
         let serviceCostTotal = 0;
         snapshot.forEach((doc) => {
           serviceCostTotal = serviceCostTotal + parseInt(doc.data().serviceCost, 10);
         })
         // Do whatever you want with serviceCostTotal
    });

You need to loop over all the docs returned by the query and sum up the value of the field.您需要遍历查询返回的所有文档并总结该字段的值。 Note that it is stored as a String, so you need to convert it to a number.请注意,它存储为字符串,因此您需要将其转换为数字。

I make the assumption that the values of serviceCost are always an integer.我假设serviceCost的值总是一个整数。 If they are a float, it is recommended to use a library like Big.js when summing up.如果是浮点数,建议在总结时使用Big.js之类的库。


Note however that, with this approach, each time you want to calculate the sum you read the entire services subcollection for the user, which can be costly, depending on the number of Firestore documents in this subcollection.但是请注意,使用这种方法时,每次您要计算总和时,您都会为用户读取整个services子集合,这可能会很昂贵,具体取决于此子集合中 Firestore 文档的数量。 You could maintain an "external" sum in other Firestore document(s), as Methkal Khalawi proposes in his answer.正如 Methkal Khalawi 在他的回答中提出的那样,您可以在其他 Firestore 文档中维护“外部”总和。 Have a look at the Distributed Counters concept , for some inspiration for such a solution.查看Distributed Counters 概念,以获得此类解决方案的一些灵感。

I guess I'm late but after some extensive research, there is a way in which we can fetch specific fields from firestore.我想我来晚了,但经过一些广泛的研究,有一种方法可以让我们从 firestore 中获取特定的字段。 We can use the select keyword, you're query would be somthing like (I'm using a collection for a generalized approach):我们可以使用select关键字,您的查询类似于(我正在使用集合作为通用方法):

const result = await firebase.firestore().collection("users").select("serviceCost").get();

Where we can access the result.docs to further retrieved the returned filtered result.我们可以在哪里访问result.docs以进一步检索返回的过滤结果。 Thanks!谢谢!

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

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