Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
Extract field from mongodb query result set

How to extract json field from query response in node js?

I have a query:-

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

Allposts:-

  {
    _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

I want to extract the totalCount field and store its value how to do this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you get only totalCount field from query response, you can add .project() in query as below

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

0

Assuming totalCount value will be same in all the objects, you can extract the first value using first index

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda