Estoy trabajando en un problema de rendimiento de una función, que tarda 15 segundos en responder, lo que hace una solicitud a Firebase para todos los documentos que son "ErrorID" "==" "0"
El problema es que hay muchos documentos y son amables de objetos muy grandes, y solo necesito DOS CAMPOS (Order and Amount)
de cada documento, hay alguna forma de solicitar solo esos dos campos que cumplen la condición?
Algo como :
firestore.collection("purchases").where("ErrorID", "==", "0").get(Order, Amount);
La función de la que estoy hablando:
const totalEarn = async (req, res, next) => { const DAY = 86400000; const WEEK = 604800016; const MONTH = 2629800000; try { let snap = await firestore.collection("purchases").where("ErrorID", "==", "0").get(); // CONDITION let totalToday = 0; let totalYesterday = 0; let totalLastWeek = 0; let totalLastMonth = 0; let now = Date.now(); let Yesterday = now - 86400000; await snap.forEach((doc) => { // THIS FOR EACH TAKES TOO MUCH TIME let info = doc.data(); let time = info.Order.split("-")[2]; // FIRESTORE FIELD -> ORDER let amount = info.AmountEur * 1; // FIRESTORE FIELD -> AMOUNT if (time > now - DAY) { totalToday = totalToday + amount; } if (time < now - DAY && time > Yesterday - DAY) { totalYesterday = totalYesterday + amount; } if (time > now - WEEK) { totalLastWeek = totalLastWeek + amount; } if (time > now - MONTH) { totalLastMonth = totalLastMonth + amount; } }); res.send({ status: true, data: { totalToday: totalToday.toFixed(2), totalYesterday: totalYesterday.toFixed(2), totalLastWeek: totalLastWeek.toFixed(2), totalLastMonth: totalLastMonth.toFixed(2), }, }); } catch (error) { res.status(410).send({ status: false, error: "some error occured counting the numbers", e: error.message, }); } };
El documento del que estoy hablando
Si usa el cliente Firestore Node.JS o Firebase Admin SDK, puede usar select()
para seleccionar campos:
import { Firestore } from "@google-cloud/firestore"; const firestore = new Firestore(); const snap = firestore .collection("purchases") .where("ErrorID", "==", "0") .select("Order", "Amount") .get();