Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
Extraer el campo del conjunto de resultados de la consulta mongodb

¿Cómo extraer el campo json de la respuesta de consulta en el nodo js?

Tengo una consulta:-

 const Allposts = await Post.aggregate([pipeline])

Todos los mensajes:-

 { _id: 1, type: 'A', source: 'B', status: 'C', totalCount: 7, createdAt: 2022-04-13T17:12:28.097Z, updatedAt: 2022-04-13T17:12:28.097Z, __v: 0 }, { _id: 2, type: 'A', source: 'B', status: 'C', totalCount: 7, createdAt: 2022-04-13T17:12:28.097Z, updatedAt: 2022-04-13T17:12:28.097Z, __v: 0 }, { _id: 3, type: 'A', source: 'B', status: 'C', totalCount: 7, createdAt: 2022-04-13T17:12:28.097Z, updatedAt: 2022-04-13T17:12:28.097Z, __v: 0 }, and so on

Quiero extraer el campo totalCount y almacenar su valor, ¿cómo hacer esto?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Si obtiene solo el campo totalCount de la respuesta de la consulta, puede agregar .project() en la consulta como se muestra a continuación

 const Allposts = await Post.aggregate().project("totalCount -_id");
about 4 years ago · Juan Pablo Isaza Report

0

Suponiendo que el valor totalCount será el mismo en todos los objetos, puede extraer el primer valor usando el primer índice

 let tc = Allposts[0].totalCount; 

 let data = [{ _id: 1, type: 'A', source: 'B', status: 'C', totalCount: 7, createdAt: '2022 - 04 - 13 T17: 12: 28.097 Z', updatedAt: '2022 - 04 - 13 T17: 12: 28.097 Z', __v: 0 }, { _id: 2, type: 'A', source: 'B', status: 'C', totalCount: 7, createdAt: '2022 - 04 - 13 T17: 12: 28.097 Z', updatedAt: '2022 - 04 - 13 T17: 12: 28.097 Z', __v: 0 }, { _id: 3, type: 'A', source: 'B', status: 'C', totalCount: 7, createdAt: '2022 - 04 - 13 T17: 12: 28.097 Z', updatedAt: '2022 - 04 - 13 T17: 12: 28.097 Z', __v: 0 } ] let tc = data[0].totalCount; console.log(tc);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!