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

156
Visualizações
How to get list of items with same id using find()

I'm getting data from my back-end the goal is when I want to click on the button of a specific id I want to get list of data with same id , because I need it to affect the data in charts NB : I couldn't change the form of data it should return like that cuz I'm using mongo db functions such as $group and $count

This is my data from back-end

Resolver [
  { _id: { _id: '123456789', totaloutcome: 'DONE' }, count: 4 },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_APPROVED_ONLINE' },
    count: 27
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_CANCELLED' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_UNKNOWN' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_DECLINED' },
    count: 15
  }
]

for who want to see my back-end code I'm using NestJs

async getOutcomeCount() {
    return await this.tpeModel.aggregate([
      {
        $lookup: {
          from: 'logs',
          localField: 'terminalId',
          foreignField: 'terminalId',
          as: 'resultData',
        },
      },
      { $unwind: '$resultData' },

      {
        $group: {
          _id: {
            _id: '$resultData.terminalId',
            totaloutcome: '$resultData.outcome',
          },
          count: { $sum: 1 },
        },
      },
    ]);
  }

As I said I want to get list of data with _id:05000002 for e.g

Questions

  • Does I need to spread the object before making oprations of search or there is a solution to get what I need with this object to make all data (inner _id and count) in same object
    NB : Sorry for my bad english
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I'd recommend two things: first, a function that describes the value being looked for...

const innerId = obj => obj._id._id;

Then, a filter -- not a find -- to get elements of the array with that value...

const allWithId = id => resolverArray.filter(el => innerId(el) === id);

Demo...

let resolverArray = [
  { _id: { _id: '123456789', totaloutcome: 'DONE' }, count: 4 },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_APPROVED_ONLINE' },
    count: 27
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_CANCELLED' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_UNKNOWN' },
    count: 1
  },
  {
    _id: { _id: '05000002', totaloutcome: 'OUTCOME_DECLINED' },
    count: 15
  }
]

const innerId = obj => obj._id._id;

const allWithId = id => resolverArray.filter(el => innerId(el) === id);

console.log(allWithId('05000002'))

about 4 years ago · Juan Pablo Isaza Relatório

0

The JavaScript function Array.find returns only the first match if any.

You should use Array.filter instead when there is more then one match, as per docs:

method creates a new array filled with elements that pass a test provided by a function

const matchingItems = yourArray.filter(item => item.id === 123)
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