• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

319
Views
¿Hay alguna manera de seleccionar solo ciertos campos de Firestore?

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

El documento del que estoy hablando

over 3 years ago · Santiago Trujillo
1 answers
Answer question

0

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();
over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error